Simple Network Programming example for Python client and server

Source: Internet
Author: User
Tags gopher
This article mainly introduces the Python Simple Network programming, details the client and the service side of the specific implementation skills and related considerations, the need for friends can refer to the following

The examples in this article describe Python's Simple network programming. Share to everyone for your reference, as follows:

Content Directory

1. Client (client.py)
2. Service side (server.py)

I. Client (client.py)

Import Socketimport sysport = 70host = 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 ():  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) 0i fake (NULL) 0iThis server has a lot of information of historic interest, fake (null) 0ifunny, or just P Lain entertaining--all presented in Gopher. Fake (NULL) 0iThere is many mirrors here's rare or valuable files with the fake (null) 0iaim to preserve them in case Their host disappears. Please READ fake (NULL) 0i "about this Server" for IMPORTANT NOTES and LEGAL information. Fake (null) 0i fake (null) 00About This server/about this Server.txt gopher.quux.org +1archives/archives gopher. quux.org +1books/books gopher.quux.org +1communication/communication gopher.quux.org directory +ithis The entire text of the book Fake (NULL) 0i "We the media:grassroots journalism by the people, for the People" Fake (NU LL) 0iby Dan Gillmor in various formats.  Fake (null) 0i fake (null) 0iFeel free to download and enjoy. Fake (NULL) 01computers/computers gopher.quux.org +1current issues and Events (Updated Apr., 2002)/current gopher.quux.org +1development projects/devel gopher.quux.org (+0gopher ' s 10th anniversary  /3.0.0.txt gopher.quux.org 701Government, Politics, Law, and conflict/government gopher.quux.org-+0how to Help/how To Help.txt gopher.quux.org-+1humor and Fun/humor and fun gopher.quux.org, +1index to Quux.org/archives/index GOP her.quux.org 701internet/internet gopher.quux.org +1other Gopher servers/software/gopher/servers gopher.quux.org 701people/people gopher.quux.org +1reference/reference gopher.quux.org +1software and Downloads/software gopher.  quux.org +1the Gopher project/software/gopher gopher.quux.org 700What ' s new/whatsnew.txt gopher.quux.org 70 +

Second, the service side (server.py)

# Coding:utf-8import Sockethost = ' 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)        #每次最多只有一个等候处理print "Server is running on port%d; Press CTRL-C to terminate. "%portwhile 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 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.

jihite@ubuntu:~/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.
Related Article

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.