Python simple network programming example [client and server]
- 2020-06-01 10:16:55
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 (client.py)
2. Server (server.py)
1. Client (client.py)
import socket import sys port = 70 host = sys.argv filename = sys.argv 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(): sys.stdout.write(line)
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 client.py quux.org /
iWelcome to gopher at quux.org! 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"About This Server" FOR IMPORTANT NOTES AND LEGAL INFORMATION. fake (NULL) 0 i fake (NULL) 0 0About This Server /About This Server.txt gopher.quux.org 70 + 1Archives /Archives gopher.quux.org 70 + 1Books /Books gopher.quux.org 70 + 1Communication /Communication gopher.quux.org 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 gopher.quux.org 70 + 1Current Issues and Events (Updated Apr. 23, 2002) /Current gopher.quux.org 70 + 1Development Projects /devel gopher.quux.org 70 + 0Gopher's 10th Anniversary /3.0.0.txt gopher.quux.org 70 1Government, Politics, Law, and Conflict /Government gopher.quux.org 70 + 0How To Help /How To Help.txt gopher.quux.org 70 + 1Humor and Fun /Humor and Fun gopher.quux.org 70 + 1Index to Quux.Org /Archives/index gopher.quux.org 70 1Internet /Internet gopher.quux.org 70 + 1Other Gopher Servers /Software/Gopher/servers gopher.quux.org 70 1People /People gopher.quux.org 70 + 1Reference /Reference gopher.quux.org 70 + 1Software and Downloads /Software gopher.quux.org 70 + 1The Gopher Project /Software/Gopher gopher.quux.org 70 0What's New /whatsnew.txt gopher.quux.org 70 +
2. Server (server.py)
# 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)) clientfile.close() clientsock.close()
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 server.py
Open another terminal to connect to port 51421 of localhost.
jihite@ubuntu:~/web$ telnet localhost 51421 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Welcome, ('127.0.0.1', 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.