Python network programming-Example of TCP communication instance and socketserver framework

Source: Internet
Author: User

1. TCP is a reliable connection-oriented protocol. Before one Party sends data, a connection must be established between the two parties. The process of establishing a connection must undergo three handshakes. After the communication is complete, the connection must be removed, four handshakes are required. This is caused by the semi-closing of TCP. After sending data, one party must send a FIN to terminate the connection in this direction, a TCP connection can still send data after receiving a FIN, but few applications do this. The process of establishing and removing a TCP connection is as follows:



2. python can program TCP servers and clients. The following code is used:

Server:

Copy codeThe Code is as follows:
#! /Usr/bin/env python
Import socket
Host = "localhost"
Port = 10000
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. bind (host, port ))
S. listen (5)
While 1:
Sock, addr = s. accept ()
Print "got connection form", sock. getpeername ()
Data = sock. recv (1024)
If not data:
Break
Else:
Print data

Client:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
Import socket
Host = "localhost"
Port = 10000
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. connect (host, port ))
S. send ("hello from client ")
S. close ()

3. Use the socketserver framework to write TCP servers

The Socketserver module simplifies the compilation of network servers. It contains four types of servers. TCPServer uses the TCP protocol, UDPServer uses the UDP protocol, and there are two uncommon ones, UnixStreamServer and unixw.ramserver, these two classes are only useful in unix environments.

To use Server programming, you need to take the following steps to first create a request handle class, which inherits from the BaseRequestHandler class, create this class, override its handle method, and then instantiate the server class, pass the host name, port number, and handle class to it, and then call server_forever () to process the request.

Servers using the socketserver framework:

Copy codeThe Code is as follows:
Import SocketServer
Host =''
Port = 10000
Class Handler (SocketServer. StreamRequestHandler ):

Def handler (self ):
Addr = self. request. getpeername ()
Print "got connection from", addr
Self. wfile. write ("connected ")

Server = SocketServer. TCPServer (host, port), Handler)
Server. serve_forever ()

The above socketserver server can only process one request. To process multiple requests, you can use forking or threading to implement multi-process or multi-thread servers. The following is the server code using forking and threading:

Forking Server:

Copy codeThe Code is as follows:
From SocketServer import TCPServer, ForkingMixIn, StreamRequestHandler
Class Server (ForkingMixIn, TCPServer): pass
Class Handler (StreamRequestHandler ):

Def handle (self ):
Addr = self. request. getpeername ()
Print "got connection from", addr
Self. wfile. write ('connected ')

Server = Server (''. 10000), Handler)
Server. serve_forever ()

Multi-threaded servers:

Copy codeThe Code is as follows:
From SocketServer import TCPServer, ThreadingMixIn, StreamRequestHandler
Class Server (ThreadingMixIn, TCPServer): pass

Class Handler (StreamRequestHandler ):
Def handle (self ):
Addr = self. request. getpeername ()
Print "got connection from", addr
Self. wfile. write ("connected ")

Server = Server ('', 10000), Handler)
Server. serve_forever ()

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.