Home > other >  About socketserver communication between threads
About socketserver communication between threads

Time:10-27

This procedure is mainly used socketserver listening 2 port, a 9999, a 10000
Design is, regardless of which port connection in class attribute conn_pool stored in the
The program will connect port 9999, the data received, sent to the port 10000 connection, connect the port 10000, receiving data, sent to the port 9999 connections,

The question now is, class attribute conn_pool only go in connection with the port, such as when 10000999 9 port each have a connection, but in def setup (self) : to back the print (MySelfServer. Conn_pool. Qsize ()) are 1, (right should be 2) what's the problem?



# used to create the server module
The import socketserver
The from multiprocessing import Process
From the queue the import queue


# https://www.cnpython.com/qa/66814
# http://www.cppcns.com/jiaoben/python/255861.html

# the first step to create a server class, inheritance BaseRequestHandler class
The class MySelfServer (socketserver BaseRequestHandler) :
Conn_pool=None

Def __init__ (self, conn_pool) :
MySelfServer. Conn_pool=conn_pool

Def __call__ (self, request, client_address, server) :
H=MySelfServer (MySelfServer conn_pool)
Socketserver. BaseRequestHandler. __init__ (h, request, client_address, server)

Def setup (self) :
Self. Request. Sendall (" Successfully connected to the server!" The encode (encoding="utf-8"))
# the self. The request is equivalent to a conn
# to join connection pool
Print (id (self. Request))
MySelfServer. Conn_pool. Put (self. Request)
Print (MySelfServer. Conn_pool. Qsize ())


Def finish (self) :
Print (" This client is cleared ")


Def remove (self) :
Print (" A client is offline ")
MySelfServer. Conn_pool. Get (self. Request)

# rewrite BaseRequestHandler handle method in the class, write directly in their own to create a class to
Def handle (self) : # inside the content for the server to the client all interactions
Try:
While True:
# receive data
Self. Data=https://bbs.csdn.net/topics/str (self. Request. Recv (1024). Strip (), encoding="utf-8")

If len (self. Data)==0:
The continue
# print client IP address, and send the data, it may ask why there are self. Client_address the parameters, the
in the parent class constructorPrint (f '{self. Client_address [0]} to send information, content is: {self. Data}')

For I in MySelfServer. Conn_pool:
# connect content sent to the port of all connections, but except for the current connection, because it is, a bit like a chat room
If I!=the self. The request and the self. Request. Getsockname () [1]==i.g etsockname () [1] :
I. endall (bytes (self) data, encoding="utf-8"))
Except the Exception as e:
Print (e)
Print (self client_address, "disconnects")
Finally:
The self. The request. The close ()


If __name__=="__main__" :
The HOST="0.0.0.0"
PORT_1=9999
PORT_2=10000
Server1=None
Server2=None
# connection pool

# captain is thread-safe, bring their own lock
G_conn_pool=Queue ()

# allows the server to rebind a previously used by the port number,
Socketserver. ThreadingTCPServer. Allow_reuse_address=True
Step # 2 to instantiate one of four classes and introduced to the address of the server and the server class, create your own above here own instantiation TCPServer
Server1=socketserver. ThreadingTCPServer ((HOST, PORT_1), MySelfServer (g_conn_pool))

Server2=socketserver. ThreadingTCPServer ((HOST, PORT_2), MySelfServer (g_conn_pool))

# allow reuse address
Server1. Allow_reuse_address=True
Server2. Allow_reuse_address=True

P=the Process (target=server2 serve_forever, args=())
P. tart ()

# server1 need on p. tart after startup or it will block the process, server2 wouldn't start
Server1. Serve_forever ()

# main thread wait for the end of the p thread to exit the program
P.j oin ()
  • Related