Python simple network programming example [client and server]

  • 2020-06-01 10:16:55
  • OfStack

This article demonstrates simple network programming for Python as an example. I will share it with you for your reference as follows:

Table of contents

1. Client (
2. Server (

1. Client (

import socket
import sys
port = 70
host = sys.argv[1]
filename = sys.argv[2]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fd = s.makefile("rw", 0)
fd.write(filename + "\n")
for line in fd.readlines():

The program establishes an Socket via socket.socket (), and the parameter tells the system that one Internet Socket is needed for TCP communication. The program then links to the remote host name and provides the file name. Finally get the response and print it out on the screen.


python /

According to

iWelcome to gopher at! fake  (NULL) 0
i  fake  (NULL) 0
iThis server has a lot of information of historic interest, fake  (NULL) 0
ifunny, or just plain entertaining -- all presented in Gopher. fake  (NULL) 0
iThere are many mirrors here of rare or valuable files with the fake  (NULL) 0
iaim to preserve them in case their host disappears. PLEASE READ  fake  (NULL) 0
i  fake  (NULL) 0
0About This Server /About This Server.txt 70 +
1Archives  /Archives 70 +
1Books /Books 70 +
1Communication /Communication 70 +
iThis directory contains the entire text of the book  fake  (NULL) 0
i"We the Media: Grassroots Journalism by the People, for the People"  fake  (NULL) 0
iby Dan Gillmor in various formats. fake  (NULL) 0
i  fake  (NULL) 0
iFeel free to download and enjoy.  fake  (NULL) 0
1Computers /Computers 70 +
1Current Issues and Events (Updated Apr. 23, 2002) /Current 70 +
1Development Projects  /devel 70 +
0Gopher's 10th Anniversary /3.0.0.txt 70
1Government, Politics, Law, and Conflict  /Government 70 +
0How To Help  /How To Help.txt 70 +
1Humor and Fun /Humor and Fun 70 +
1Index to Quux.Org /Archives/index 70
1Internet  /Internet 70 +
1Other Gopher Servers  /Software/Gopher/servers 70
1People /People 70 +
1Reference /Reference 70 +
1Software and Downloads /Software 70 +
1The Gopher Project /Software/Gopher 70
0What's New /whatsnew.txt 70 + 

2. Server (

# coding: utf-8
import socket
host = ''
port = 51421
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)        # At most 1 Waiting to be processed 
print "Server is running on port %d; press Ctrl-C to terminate." %port
while 1:
  clientsock, clientaddr = s.accept()
  clientfile = clientsock.makefile('rw', 0)
  clientfile.write("Welcome, " + str(clientaddr) + "\n")
  clientfile.write("Please enter a string: ")
  line = clientfile.readline().strip()
  clientfile.write("You entered %d characters. \n" %len(line))

Create an socket, set it to reusable (reusable), bind the port number 51421 (optionally any value greater than 1024), call the listen () function, start waiting for requests from the client, and set the maximum of 1 waiting link.

The main loop starts calling the a.accept () function, and the program stops immediately after connecting to one client to receive user input.

Run 1 example

First run


Open another terminal to connect to port 51421 of localhost.

jihite@ubuntu:~/web$ telnet localhost 51421
Connected to localhost.
Escape character is '^]'.
Welcome, ('', 59853)
Please enter a string: mm
You entered 2 characters.
Connection closed by foreign host.

For more information about Python, please see the following topics: Python Socket programming skills summary, Python function skills summary, Python string manipulation skills summary, Python introduction and advanced classic tutorial and Python file and directory manipulation skills summary.

I hope this article is helpful for you to design Python program.

Related articles: