Examples of TCP communications and socketserver frameworks used in Python network programming _python

Source: Internet
Author: User
Tags in python

1.TCP is a reliable connection-oriented protocol, before a party sends data, must establish a connection between the two sides, the establishment of the process requires three handshake, after the communication is completed to remove the connection, need to go through four handshake, which is caused by the semi-shutdown TCP, One side sends a fin to terminate the connection after the data has been sent, and a TCP connection can still send data after receiving a fin, but the application rarely does, and the following is the process of establishing and dismantling the TCP connection:



2.python can implement TCP server and client programming, the following is code:

Server side:

Copy Code code 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 Code code 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. Writing a TCP server using the Socketserver framework

The Socketserver module simplifies the authoring of Web servers, it contains four server classes, TCPServer uses TCP protocols, Udpserver uses UDP protocols, and there are two less frequently used, That is, Unixstreamserver and Unixdatagramserver, these two classes are only useful in UNIX environments.

Using server programming, you need to take steps to first create a request handle class that inherits from the Baserequesthandler class, builds the class, overrides its handle method, instantiates the server class, passes the hostname, the port number, and the handle class to it, and then calls the Server_ Forever () method to process the request.

Servers using the Socketserver framework:

Copy Code code 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 handle one request, and if you want to handle multiple requests, you can implement multiple processes or multi-threaded servers with forking or threading implementations. The following are server code that uses forking and threading:

Server using forking:

Copy Code code as follows:

From Socketserver import Tcpserver,forkingmixin,streamrequesthandler
Class Server (forkingmixin,tcpserver):p
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 ()

Using multi-threaded servers:

Copy Code code as follows:

From Socketserver import Tcpserver,threadingmixin,streamrequesthandler
Class Server (threadingmixin,tcpserver):p

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.