Based socket tcp protocol

#serve server
Import Socket
SK = socket.socket () # instance object
sk.bind (( '127.0.0.1', 8080 )) # is provided within the set of IP and port using the bind method
sk.listen () # monitor information
conn , addr = sk.accept () # received information
the while True:
RET = conn.recv (1024) .decode ( 'UTF-. 8') # receive and decode information is provided directly received an integer multiple of the size of 1024, 1024 can
Print (RET)
IF RET == 'bYE':
conn.send (bytes ( 'goodbye', encoding = 'utf-8 ')) # transmit information, the type of information bytes must
BREAK
RET = INPUT ( '>> > ')
conn.send (bytes (RET, encoding =' UTF-. 8 '))
conn.Close ()
sk.close ()

 

#client client
Import Socket
SK = socket.socket ()
sk.connect (( '127.0.0.1', 8080)) disposed within the set of IP and port ##, using the method connect
the while True:
info = INPUT ( '>>> ')
sk.send (bytes (info, encoding =' UTF-. 8 ')) # coding type information transmitted bytes
ret = sk.recv (1024) .decode ( ' utf-8 ') # decryption
Print (RET)
SK. close ()

Guess you like

Origin www.cnblogs.com/account/p/11276647.html