Python network Programming--a simple example

Source: Internet
Author: User
Tags gopher

Client (client.py)
 import   socket  import   Sysport  = 70host  = Sys.argv[1]filename  = Sys.argv[2]s  =< Span style= "color: #000000;" > Socket.socket (socket.af_inet, socket. Sock_stream) S.connect ((host, port)) fd  = s.makefile ( " RW   , 0) fd.write (filename  + Span style= "color: #800000;" > " \n  "   for  line in   Fd.readlines (): Sys.stdout.write (line)  

The program establishes a socket via Socket.socket (), which tells the system to require an Internet socket for TCP communication. The program then links the remote host name and provides the file name. The final response is printed on the screen after it is received.

Test

Python client.py quux.org/

Show

Iwelcome to Gopher at Quux.org!fake (null) 0ifake (NULL) 0iThis server had a lot of information of historic interest,fake (null ) 0ifunny, or just plain entertaining--all presented in Gopher.fake (NULL) 0iThere is many mirrors here of rare or valuabl  e files with Thefake (NULL) 0iaim to preserve them in case their host disappears. Readfake (NULL) 0i "about this Server" for IMPORTANT NOTES and LEGAL information.fake (null) 0ifake (NULL) 00About this S Erver/about this server.txtgopher.quux.org70+1archives/archivesgopher.quux.org70+1books/booksgopher.quux.org70+ 1communication/communicationgopher.quux.org70+ithis directory contains the entire text of the Bookfake (NULL) 0i "We the Media:grassroots journalism by the people, for the people "fake (null) 0iby Dan Gillmor in various formats.fake (NULL) 0ifake ( NULL) 0iFeel free to download and enjoy.fake (NULL) 01computers/computersgopher.quux.org70+1current issues and Events ( Updated APR., 2002)/currentgopher.quux.org70+1development Projects/develgopher.quux.Org70+0gopher ' s 10th anniversary/3.0.0.txtgopher.quux.org701government, Politics, law, and conflict/ Governmentgopher.quux.org70+0how to Help/how to Help.txtgopher.quux.org70+1humor and Fun/humor and Fungopher.quux.org70+1index to quux.org/archives/indexgopher.quux.org701internet/internetgopher.quux.org70+ 1Other Gopher servers/software/gopher/serversgopher.quux.org701people/peoplegopher.quux.org70+1reference/ Referencegopher.quux.org70+1software and Downloads/softwaregopher.quux.org70+1the Gopher Project/Software/  Gophergopher.quux.org700What ' s new/whatsnew.txtgopher.quux.org70+
Service side (server.py)
#Coding:utf-8ImportSockethost="'Port= 51421s=Socket.socket (socket.af_inet, socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR,1) S.bind ((host, Port)) S.listen (1)#there's only one waiting to be processed at a timePrint "Server is running on port%d, press Ctrl-c to terminate."%Port while1: 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 a socket, set it to reusable (reusable), bind the port number 51421 (optional value greater than 1024 ), call the Listen () function, start waiting for requests from the client, and set at most one link waiting to be processed.

The main loop begins with the a.accept () function call, and the program stops immediately after connecting to a client, receiving input from the user.

Run an example

Run server.py First

Python server.py

Open a different terminal, connect the 51421 port of localhost.

[Email protected]:~/web$ telnet localhost 51421Trying 127.0.0.1...Connected to localhost. Escape character is ' ^] '. Welcome, (' 127.0.0.1 ', 59853) Please enter a string:mmyou entered 2 characters. Connection closed by foreign host.

  

Python network Programming--a simple example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.