Python check-multi-thread socket server
In the sunny afternoon, think about writing from Java to Python recently. Just turn on your computer and try the difference between Python and Java ~ I remember that when I was in my sophomore year, I started learning Java at that time. What I was most interested in was the socket section in the last chapter of the Java book, the first time I saw the appearance of opening multiple black-white shell interfaces at the same time, I was so excited. T.T ~ At that time, I still don't know what multithreading is. One client will stop other clients from working ..... I think multithreading is the most troublesome thing to learn .... It took me one afternoon to build a multi-threaded socket program ~~ To describe what I hate most about Python, it must be that it does not have {} In a java method. Instead, it is actually indented .... Really nice and uncomfortable .... This is where T.T is thrown. After reading the Python socket tutorial, we found that it is similar to Java and still creates a connection. The client and server communicate with each other through the send and recv methods, and finally close the connection, in order to prevent other clients from being suspended due to a connection between a client and the server, I also specially added the Python multithreading to ensure that this phenomenon does not occur. For the multi-threading of Python, I prefer to create threading. the sub-class of Thread to wrap a Thread object. Maybe this is related to Java I often write ~~ If you are more familiar with this method, paste the server and client code: copy the Code # coding = UTF-8 import socketimport threading, getopt, sys, string opts, args = getopt. getopt (sys. argv [1:], "hp: l:", ["help", "port =", "list ="]) # Set the default maximum number of connections and port number, the default value list = 50 port = 8001def usage () will be used when parameters are not passed in using commands (): print ""-h -- help print the help-l -- list Maximum number of connections-p -- port To monitor the port number "for op, value in opts: if op in ("-l", "-- list"): list = String. atol (value) elif op in ("-p", "-- port"): port = string. atol (value) elif op in ("-h"): usage () sys. exit () def jonnyS (client, address): try: # Set the timeout value to client. settimeout (500) # size of the received data buf = client. recv (2048) # Return the received information to the client as is. send (buf) # After the timeout, it is displayed that the consumer t socket is exited. timeout: print 'time out' # Close the client Connected to the client. close () def main (): # create a socket object. Call the socket constructor # AF_INET is the IP address family, And SOCK_STREAM is the stream socket sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # bind the socket to the specified address. The first parameter is the IP address, and the second parameter is the port number sock. bind ('localhost', port) # set the maximum number of connections sock. listen (list) while True: # The server socket waits for the client to request a connection through the socket accept method, address = sock. accept () thread = threading. thread (target = jonnyS, args = (client, address) thread. start () if _ name _ = '_ main _': copy the code in main () to prevent the number of customers A large amount of requests will cause the customer to wait. I set the timeout time. Although this problem cannot be solved in a more cost-effective manner, it can prevent some clients from occupying the number of connections without working, it's a clever solution ~ In addition, you can easily control the listening port number and the maximum number of connections allowed by passing in parameters. Although you have not verified how many processes can be started by the system, it is sufficient for one exercise, if you want anyone to know, please leave a message and let me know ~ The following is the client: copy the Code # coding = utf-8import getopt, socket, sys, string opts, args = getopt. getopt (sys. argv [1:], "hi: p:", ["help", "ip =", "port ="]) # Set the default ip address and port number, the default value host = "localhost" port = 8001def usage () is used when parameters are not passed in using commands (): print ""-h -- help print the help-I -- ip Enter the IP address to connect-p -- port Enter the port number to connect "for op, value in opts: if op in ("-I", "-- ip"): host = value elif op in ("-p "," -- Port "): port = string. atol (value) elif op in ("-h"): usage () sys. exit () def main (): # create a socket object. Call the socket constructor # AF_INET is the IP address family, And SOCK_STREAM is the stream socket sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # Set the ip address and port number of the server to be connected to sock. connect (host, port) # The client inputs a string to the server message = raw_input ("inupt:") # pdb. set_trace () sock. send (message) print 'serveroupt: '+ sock. recv (2048) # Close the connection to the server sock. close () if _ name _ = '_ main _': copying the code client from main () is much simpler. It is easy to read user input, sending the code to the server and receiving it back is a simple function. After encoding is set, it is also possible to set Chinese characters or something ~ It is worth noting that the sending size set in the recw () method will report an error if it is too small ~