Python simple network programming example [client and server], python network programming example

Source: Internet
Author: User

Python simple network programming example [client and server], python network programming example

This example describes Python simple network programming. We will share this with you for your reference. The details are as follows:

Content directory

1. client. py)
2. server (server. py)

1. 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 uses socket. socket () to establish a Socket. The parameter tells the system that an Internet Socket is required for TCP communication. Then the program links to the remote host name and provides the file name. Finally, the response is printed on the screen.

Test

python client.py quux.org /

Display

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 plain entertaining -- all presented in Gopher. fake  (NULL) 0iThere are many mirrors here of 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 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) 0i"We the Media: Grassroots Journalism by the People, for the People"  fake  (NULL) 0iby Dan Gillmor in various formats. fake  (NULL) 0i  fake  (NULL) 0iFeel free to download and enjoy.  fake  (NULL) 01Computers /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 701Government, 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 701Internet  /Internet  gopher.quux.org 70 +1Other Gopher Servers  /Software/Gopher/servers  gopher.quux.org 701People /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 700What's New /whatsnew.txt  gopher.quux.org 70 + 

Ii. server (server. py)

# Coding: utf-8import sockethost = ''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 one waiting for processing at a time 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 (any value greater than 1024), call the listen () function, and start waiting for requests from the client, at the same time, you can set up to one link waiting for processing.

The main loop starts to call the. accept () function. After the program connects to a client, it immediately stops and receives user input.

Run an example

First run server. py

python server.py

Open another terminal to connect to port 51421 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.

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.