Python network programming details, python Network Programming

Source: Internet
Author: User

Python network programming details, python Network Programming

1. A server is a series of hardware or software that provides the required "service" for one or more clients (service users ". It has the sole purpose of waiting for client requests, responding to them (providing services), and then waiting for more requests.

2. The client/server architecture can be applied to both computer hardware and computer software.

3. Before the server responds to the client, a communication node is created to enable the server to listen to requests.

I. Socket: Communication endpoint

1. Socket

Socket is the data structure of the computer network. It embodies the concept of "Communication endpoint" described in the previous section. Network applications must create sockets before any type of communication starts.

There are two types of sockets: file-based and network-oriented.

2. socket address: Host-Port Pair

If a socket is like a phone jack-some infrastructure that allows communication, the host name and port number are like a combination of area code and phone number. Valid port number range: 0-65535 (the port number smaller than 1024 is reserved for the System)

3. connection-oriented sockets and connectionless sockets

Connection-oriented. A connection must be established before communication. The main protocol for implementing this connection type is TCP (Transmission Control Protocol)

No connection. You do not need to establish a connection before communication. The main protocol is UDP (User Datagram Protocol)

Ii. Network Programming in Python

1. socket () module functions

Create a TCP/IP socket: tcpSock = socket. socket (socket. AF_INEF, socket. SOCK_STREAM)

Create a UDP/IP socket: udpSock = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)

2. Common socket object methods and attributes

Name Description
Server socket Method  
S. bind () Bind the address (host name, port number pair) to the socket.
S. listen () Set and start the TCP listener
S. accept () Passively accept the TCP client connection and wait until the connection reaches (blocking)
Client socket Method  
S. connect () Actively initiate TCP server connection
S. connect_ex () The extended version of connect will return an error code instead of throwing an exception.
Common socket Method  
S. recv () Accept TCP messages
S. send () Send TCP messages
S. sendall () Completely send TCP messages
S. recvfrom () Receive UDP messages
S. shutdown () Close connection
S. close () Disable socket

3. Create a TCP Server

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 () # receive client connection comm_loop: # communication loop cs. recv ()/cs. send () # cs. close () # close the client socket ss. close () # disable server socket (optional)
#! /Usr/bin/env python # TCP timestamp server from socket import * from time import ctimeHOST = ''PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) tcpSerSock = socket (AF_INET, SOCK_STREAM) tcpSerSock. bind (ADDR) tcpSerSock. listen (5) while True: print ('Waiting for connecting... ') tcpClisock, addr = tcpSerSock. accept () print ('... connected from: ', addr) while True: data = tcpClisock. recv (BUFSIZ) if not data: break tcpClisock. send ('[% s] % s' % (bytes (ctime (), 'utf-8'), data) tcpClisock. close () tcpSerSock. close ()

4. Create a TCP client

cs = socket()cs.connect()comm_loop:  cs.send()/cs.recv()cs.close()
#! /Usr/bin/env python # TCP timestamp client from socket import * HOST = '2017. 0.0.1 'port = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) tcpClisock = socket (AF_INET, SOCK_STREAM) tcpClisock. connect (ADDR) while True: data = input ('>') if not data: break tcpClisock. send (data) data = tcpClisock. recv (BUFSIZ) if not data: break print (data. decode ('utf-8') tcpClisock. close ()

5. Create a UDP Server

ss = socket()ss.bind()inf_loop:  cs = ss.recvfrom()/ss.sendto()ss.close()
#! /Usr/bin/env python # UDP timestamp server from socket import * from time import ctimeHOST = ''PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) udpSerSock = socket (AF_INET, SOCK_DGRAM) udpSerSock. bind (ADDR) while True: print ('Waiting for message... ') data, addr = udpSerSock. recvfrom (BUFSIZ) udpSerSock. sendto ('[% s] % s' % (ctime (), data), addr) print ('... stored ed from and returned to: ', addr) udpSerSock. close ()

6. Create a UDP client

cs = socket()comm_loop:  cs.send()/cs.recvfrom()cs.close()
#! /Usr/bin/env python # UDP timestamp client from socket import * HOST = 'localhost' PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) udpClisock = socket (AF_INET, SOCK_DGRAM) while True: data = input ('>') if not data: break udpClisock. sendto (data, ADDR) data, ADDR = udpClisock. recvfrom (BUFSIZ) if not data: break print (data) udpClisock. close ()

The above is all the details about Python network programming in this article, and I hope to help you. For details about how to resolve enumerate function code in Python, how to determine the difference between type and isinstance in python, and how to use the session object in the requests library in python, please feel free to leave a message if you have any questions, if there is any problem, I will change it...

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.