Examples of TCP communication in python network programming and the use of the socketserver framework

  • 2020-04-02 13:36:18
  • OfStack

1. TCP is a connection-oriented, reliable protocols, before sending data from one side, must establish a connection between the two sides, need to go through the process of building three times handshake, after the completion of the communication to dismantle the connection, takes four to shake hands, this is caused by the TCP half closed, one party after the complete data to be sent to end the direction of a FIN connection, a TCP connection after receiving a FIN can still send data, but applications rarely do this, the following is the process of TCP connection establishment and removal:

< img SRC = "border = 0 / / img.jbzj.com/file_images/article/201404/2014425104756975.jpg? 201432510488 "> < img SRC =" border = 0 / / img.jbzj.com/file_images/article/201404/2014425104912087.jpg? 2014325104924 ">

2. Python can achieve TCP server and client programming, the following is the code:

Server-side:


#!/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:


#!/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. Write a TCP server using the socketserver framework

      The Socketserver module simplifies writing network servers. It contains four server classes, TCPServer using TCP, UDPServer using UDP, and two less commonly used ones, namely UnixStreamServer and UnixDatagramServer, which are only useful in Unix environments.

      Using server programming, you first create a request handle class that inherits from the BaseRequestHandler class, create the class, override its handle method, then instantiate the server class, pass it the hostname, port number, and handle class, and then call the server_forever() method to handle the request.

    Server using the socketserver framework:


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 socketserver server server above can only handle one request, and if you have multiple requests to handle, you can implement a multi-process or multi-threaded server using forking or threading. Here's the server code using forking and threading:

Server using forking:


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()

Using multi-threaded servers:


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 articles: