python TCP multi-client connections

Python TCP server code:
# coding=utf-8
# !/usr/bin/env python
 
 
from socket import *
from time import ctime
import threading
import time

HOST = '' # host address
PORT = 3046 # port
BUFSIZ = 1024 # The buffer size
ADDR = (HOST, PORT) # address and port
 
tcpSerSock = socket (AF_INET, SOCK_STREAM) # create a TCP socket
tcpSerSock.bind (ADDR ) bound address and port #
tcpSerSock.listen (5) # is the maximum number of client connections. 5
SOCKS = [] # Socket place each client
# Create a thread traversing sock, the reception data
DEF handle ():
     the while True:
         for S SOCKS in:
             the try:
                 data = s.recv (BUFSIZ) # program execution continues down here
             except exception, e: # receiving no exception trap
                 #print "reception anomaly!";
                 continue
             IF not data: receiving no data is shifted # In addition to the client reference
                 s.send ( '[% s], % s'% (ctime (), "")) # Need to respond before the client is disconnected, or the client can not establish a connection again
                 = s.getpeername info ()
                 Print "client", info, "disconnected!"
                 S.CLOSE () # disconnect the connection cilent

                socks.remove (s) # remove the connector from the array reference cilent
                 Continue
             s.send ( '[% S], S%'% (the ctime (), Data)) # data received the returned data
             Print Data;
          
 
T = threading.Thread (target = handle) # child thread
IF __name__ == '__main__':
     t.start () # start the thread
     Print 'connecting ... Waiting for'
     the while True: # cycle monitor connected
         clientSock, addr = tcpSerSock. accept () # wait client connection
         print 'connected from:', addr # print client address and port information
         clientSock.setblocking (0) # following is a non-blocking
         socks.append (clientSock) # save the client references to socks array

TCP client connection test tools do:


Netcom Screenshot 20190601161350

Disconnect:

Netcom Screenshot 20190601161541


Guess you like

Origin www.cnblogs.com/Yfw-Crayy/p/10959989.html