Python-based TCP and UDP (detailed description) and pythontcpudp

Source: Internet
Author: User

Python-based TCP and UDP (detailed description) and pythontcpudp

In python, UDP and TCP communication is implemented through socket (socket. There are two types of sockets for connection and connectionless, namely TCP socket and UDP socket.

TCP Communication Model

Create a TCP Server

Pseudocode:

Ss = socket () # create a server socket ss. bind () # bind the socket to the address to ss. listen () # Listening to inf_loop: # server infinite loop cs = ss. accept () # accept client connection comm_loop: # communication loop cs. recv ()/cs. send () # receive/send a dialog) cs. close () # close the client socket ss. close () # close server socket # (optional)

TCP timestamp Server:

Import socketfrom time import ctimeHOST = ''PORT = 8099 BUFSIZE = 1024 ADDR = (HOST, PORT) tcpSerSock = socket. socket (socket. AF_INET, socket. SOCK_STREAM) tcpSerSock. bind (ADDR) tcpSerSock. listen (5) while True: print ("waiting for connection ...... ") tcpCliSock, addr = tcpSerSock. accept () print ("... received connection: ", addr) while True: data = tcpSerSock. recv (BUFSIZE) if not data: break tcpCliSock. send ('[% s] % s' % (bytes (ctime (), 'utf-8'), data) tcpCliSock. close () tcpSerSock. close ()

Create a TCP client:

Pseudocode:

Cs = socket () # create a client socket cs. connect () # Try to connect to the server comm_loop: # communication loop cs. send ()/cs. recv () # Send/receive cs. close () # close the client socket

TCP timestamp client:

import socketHOST = 'localhost'PORT = 8099BUFSIZE = 1024ADDR = (HOST, PORT)tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)tcpCliSock.connect(ADDR)while True: data = input("> ") if not data:  break tcpCliSock.send(data) data = tcpCliSock.recv(BUFSIZE) if not data:  break print(data)tcpCliSock.close()

UDP Communication Model

Create a UDP Server

Pseudocode:

Ss = socket () # create a server socket ss. bind () # bind the server socket inf_loop: # server infinite loop cs = ss. recvfrom ()/ss. sendto () # disable (receive/send) ss. close () # close the server socket

UDP timestamp Server:

Import socketfrom time import ctimeHOST = ''PORT = 8099 BUFSIZE = 1024 ADDR = (HOST, PORT) udpSerSock = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) udpSerSock. bind (ADDR) while True: print ("waiting for connection ...... ") data, addr = udpSerSock. recvfrom (BUFSIZE) udpSerSock. sendto ('[% s] % s' % (ctime (), data), addr) print ("... received connection: ", addr) udpSerSock. close ()

Create a UDP client

Pseudocode:

Cs = socket () # create client socket comm_loop: # communication loop cs. sendto ()/cs. recvfrom () # Send/receive cs. close () # close the client socket

UDP timestamp client:

import socketHOST = 'localhost'PORT = 8099BUFSIZE = 1024ADDR = (HOST, PORT)udpCliSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)udpCliSock.connect(ADDR)while True: data = input("> ") if not data:  break udpCliSock.sendto(data, ADDR) data, ADDR = udpCliSock.recvfrom(BUFSIZE) if not data:  break print(data)udpCliSock.close()

The above article is based on the TCP and UDP (detailed description) in python. I hope to give you a reference and support for the customer's house.

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.