Python Core programming (Network programming)

Source: Internet
Author: User

1. Python socket module built-in method

2, the TCP server pseudo-code

3. TCP Client Pseudo-code

4. Socket Module Properties

5. A simple TCP client and service-side code:
# encoding:utf-8from Socket Import *from time import ctimefrom datetime import *# define TCPServer listening port number host = ' 0.0.0.0 ' port = 2 1567ADDR = (HOST, PORT) buffsize=1024# Initializes a TCP Sockettcpsvrsock = socket (af_inet, Sock_stream) tcpsvrsock.bind (ADDR) Tcpsvrsock.listen (5) while True:    the Print (' [%s] wait for connection ... '% (DateTime.Now ()))    Tcpclientsock, CLIENTADDR = tcpsvrsock.accept ()    print (' [%s] connect from:%s ... '% (DateTime.Now (), clientaddr)) while    True:        recievedata = tcpclientsock.recv (buffsize)        if not recievedata:            break        print (' [%s] received message:%s '% ( DateTime.Now (), Recievedata.decode (' Utf-8 '))        senddata = input (' > ')        if not senddata:        break Tcpclientsock.send (Bytes (senddata, ' utf-8 '))    tcpclientsock.close () tcpsvrsock.close ()

  

  

Client code:
# encoding:utf-8from Socket Import *from datetime import *host = ' 127.0.0.1 ' port = 21567ADDR = (HOST, PORT) buffsize=1024cl Ientsocket=socket (Af_inet,sock_stream) clientsocket.connect (ADDR) while True:    senddata=input (' > ')    if Not senddata:        break    senddata=bytes (senddata, ' utf-8 ')    clientsocket.send (senddata)    recievedata= CLIENTSOCKET.RECV (buffsize)    if not recievedata:        break    print (Recievedata.decode (' Utf-8 ')) Clientsocket.close ()

  

Python Core programming (Network programming)

Related Article

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.