socket communication process and a simple network communication program

"" " 
  Server.py
  simulated network communications server-side code
" ""
Import
socket socket packet # Import SK = socket.socket () # establish socket object Print (SK) address = ( ' 127.0.0.1 ' , 8888 ) to generate an address # tuples of information sk.bind (address) # binding address information sk.listen ( . 3 ) set the maximum number of connections # '' ' the server does not shut down, the client closes the input exit, receives a contents server connected to, for dialogue '' ' # method # Conn, addr = sk.accept () # Print (Conn) # the while True: # Data = conn.recv (1024) # IF STR (Data,' UTF8 ') =='': # conn,addr=sk.accept() # print(addr) # continue # print(str(data,'utf8')) # ssData=input('>>>') # conn.send(bytes(ssData,'utf8')) #方法二 while True: conn, addr = sk.accept() while True: try: data = conn.recv(1024) except Exception: break if str(data, 'utf8') == '': break print(str(data, 'utf8')) ssData = input('>>>') conn.send(bytes(ssData, 'utf8'))

Client code as follows: client.py

Import Socket 
SK2 = socket.socket () 
address = ( ' 127.0.0.1 ' , 8888 ) 
sk2.connect (address) 
Print (SK2)
 the while True: 
    CDATA = INPUT ( ' >>> ' )
     IF CDATA == ' Exit ' : when a client inputs # exit, the client terminates the current connection
         BREAK 
    sk2.send (bytes (CDATA, ' UTF8 ' )) 
    crdata = sk2.recv (1024 )
     Print (STR (crdata, ' UTF8 ' ))
sk2.close()

Need to turn on Allow parallet run option when debugging ,, in pycharm the Run -> Edit Configurations -> Allow parallel run (upper right corner)

Guess you like

Origin www.cnblogs.com/yangzhen-ahujhc/p/12309716.html