SoFunction
Updated on 2024-11-17

Python network programming TCP communication examples and socketserver framework use examples

It is a connection-oriented and reliable protocol, before one party sends data, a connection must be established between the two parties, the establishment process needs to go through three handshakes, and to dismantle the connection after the communication is completed, it needs to go through four handshakes, which is caused by the TCP's half-closure, one party has to send a FIN to terminate the connection in this direction after completing the data sending, a TCP connection can still send data after receiving a FIN, but applications rarely do so. A TCP connection can still send data after receiving a FIN, but applications rarely do this. The following is the process of establishing and dismantling a TCP connection:



TCP server and client programming can be implemented, here is the code:

Server-side:

Copy Code The code is as follows.

#!/usr/bin/env python
import socket
host="localhost"
port=10000
s=(socket.AF_INET,socket.SOCK_STREAM)
((host,port))
(5)
while 1:
 sock,addr=()
 print "got connection form ",()
 data=(1024)
 if not data:
  break
 else:
  print data

Client:

Copy Code The code is as follows.

#!/usr/bin/env python
import socket
host="localhost"
port=10000
s=(socket.AF_INET,socket.SOCK_STREAM)
((host,port))
("hello from client")
()

3. Using the socketserver framework to write TCP servers

Socketserver module can simplify the writing of network servers, it contains four server classes, TCPServer using the TCP protocol, UDPServer using the UDP protocol, there are two less frequently used, namely, UnixStreamServer and UnixDatagramServer, which are only useful in the unix These two classes are only useful in a unix environment.

To use server programming, you need to perform a few steps, first create a request handle class, which inherits from the BaseRequestHandler class, override its handle method after creating this class, then instantiate the server class, pass it the hostname, port number and handle class, and then call the server_forever() method to process the request.

A server that uses the socketserver framework:

Copy Code The code is as follows.

import SocketServer
host=''
port=10000
class Handler():

 def handler(self):
  addr=()
  print "got connection from",addr
  ("connected")

server=((host,port),Handler)
server.serve_forever()

The above socketserver server can only handle one request, if you want to handle more than one request, you can use forking or threading to realize multi-process or multi-threaded server. The following is the server code that uses forking and threading:

Servers that use forking:

Copy Code The code is as follows.

from SocketServer import TCPServer,ForkingMixIn,StreamRequestHandler
class Server(ForkingMixIn,TCPServer):pass
class Handler(StreamRequestHandler):

 def handle(self):
  addr=()
  print "got connection from",addr
  ('connected')

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

Servers that use multiple threads:

Copy Code The code is as follows.

from SocketServer import TCPServer,ThreadingMixIn,StreamRequestHandler
class Server(ThreadingMixIn,TCPServer):pass

class Handler(StreamRequestHandler):
 def handle(self):
  addr=()
  print "got connection from",addr
  ("connected")

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