Python network programming example-Python tutorial

Source: Internet
Author: User
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.

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.