Python network programming instance analysis, python programming instance analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.