For experienced developers, there should be a lot of programming languages. Network Programming applications are essential in these programming languages. Python is also such a programming language. We will introduce the application of Python Socket programming and network programming in detail here today.
- How Python calls the MySql Stored Procedure
- Introduction to the basic content of Python coding specifications
- Application code analysis for crawling web content using Python
- Basic Application methods for obtaining file lists in Python
- Summary of the basic concepts of the Python base64 Module
Socket: Socket
A socket is like a telephone plug-in. a host and a port are like a zone number and a phone number. A host: Where are you going to connect to? A port is connected from that port, just like most languages, python supports connection-oriented and connectionless connection.
Connection-oriented requires connection and communication. The main connection-oriented protocol is the Transmission Control Protocol (tcp). To create a tcp socket, you must specify the socket type as SOCK_STRAM, it expresses its features as a stream socket.
No connection. As the name suggests, communication can be performed without a connection. In this case, the data arrival sequence and reliability cannot be ensured. The user data packet protocol (udp) is used to implement this connection ). When creating UDP, you must specify the socket type as SOCK_DGRAM.
Create a Socket:
The Socket module function is used to create Python Socket programming. Syntax:
- Socket(Socket_family,Socket_type,protocol=0)
Socket_family is either AF_VNIX or AF_INET. Generally, AF_INET does not understand how to query the information.) Socket_type can be SOCK_STREAM or SOCK_DGRAM. If protocol is not specified, the default value is 0.
Create a TCP/IP socket:
- tcpSock = Socket(Socket.AF_INET,Socket.SOCK_STRAM)
Create a UDP/IP socket:
- udpSock= Socket(Socket.AF_INET,Socket.SOCK_DGRAM)
After creating a socket, we need to use the socket built-in method. Common Methods:
Server
- S. bind () bind the address (host, Port) to the socket
- S. listen () starts listening
- S. accept () passively accepts tcp client connections (blocking), waiting for the arrival of the Connection
Client
- S. connet () Actively initializes tcp server connections
- S. connet_ex () connet extension version. If an error occurs, the error code is returned. No exception is thrown.
- S. recv () accept tcp Data
- S. send () sends data
- S. close () close socket
The commonly used Python Socket programming method is shown above. You can also query some information.