Python network programming instance analysis, python programming instance analysis
This example describes python network programming and provides it 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.
Hi, can I give some PYTHON network programming materials or routines?
Remove it from a twisted ebook.
If it's just simple socket programming, just find some blog articles!
Python socket programming questions
Your example is okay.
While 1:
This section is an infinite loop.
As long as the client (the browser) and server (your program) still communicate, the while loop will not end.
So what you see is the effect of getting stuck, because the server is always waiting to receive data from the client.
Therefore, this while loop exits only when the server program is forcibly disabled.
Generally, if you want to send a long request to the server, you need some way to indicate the length of your data, one is the Unique End-of-string Identifier (that is, the End mark of the string, at the end of the data), one is the Size Indicator (tell the server how long your data is at the beginning of the data)
It is recommended that you carefully read the relevant documents or book pull, which is hard to explain here.
Python network programming basics are recommended:
Www.douban.com/subject/1097490/
The English version can be taken from my online storage:
Dl.getdropbox.com/..g.djvu