This article mainly introduces python network programming, which has good reference value. if you need it, you can refer to the example in this article to describe python network programming and share it with you for your reference.
The specific method is as follows:
The server code is as follows:
from SocketServer import(TCPServer as TCP, StreamRequestHandler as SRH) from time import ctime HOST = '' PORT = 21567 ADDR = (HOST, PORT) class MyRequestHandle(SRH): def handle(self): print 'connecting from ..', self.client_address self.wfile.write("[%s]:%s" % (ctime(),self.rfile.readline()) ) tcp_Server = TCP(ADDR,MyRequestHandle) print 'WAITING connecting...' tcp_Server.serve_forever()
The client code is as follows:
from socket import * HOST = 'localhost' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) while True: tcpCliSock = socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) data = raw_input('>>>') if not data: break tcpCliSock.send("%s\r\n" % data) data = tcpCliSock.recv(BUFSIZE) if not data: break print data.strip() tcpCliSock.close()
I hope this article will help you with Python programming.