In this case, the Python socket programming is described in detail. Share to everyone for your reference. Specific as follows:
Copy the Code code as follows:
Sock=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
The above code creates a socket object. The type parameter represents a socket type, which can be sock_stream (stream sockets) and Sock_dgram (datagram sockets). Af_inet indicates that the type of IP V4 was created.
Copy CodeThe code is as follows:
Socket (Address_family,type,protocol_type):
The meaning of the above three parameters is:
Address_family indicates which type of socket to set up. The most common of course is the IP protocol, af_inet. In Unix systems, Af_unix is also more commonly used to establish inter-process communication in UNIX systems.
Type is used to specify the type of communication. It is usually the establishment of a connection-oriented communication letter. Socket_dgram is message communication. If address_family is set to Af_inet, then TCP and UDP are the corresponding.
The protocol is used to specify the protocol type. This parameter is optional. They are usually 0 when establishing TCP or UDP connections. If the first parameter is af_inet, then this parameter represents the Protocol field in the IP packet.
"UDP does not differentiate between server and client. All nodes are equivalent "
The second step is to bind the socket to the specified address:
Copy the Code code as follows:
Sock.bind ((' localhost ', 7556))
The third step is to use the Listen method to listen for the request: "The parameters in the Listen method indicate the maximum number of connections acceptable"
Copy CodeThe code is as follows:
Sock.listen (5)
The fourth step is to keep receiving requests: When a connection request is received, the requests are queued and the request is rejected if the queue is full.
Copy CodeThe code is as follows:
Connection,address = Sock.accept ()
The Accept method returns a tuple containing two elements (connection,address). The first element, connection, is the new socket object that the server must communicate with the customer, and the second element address is the customer's Internet location.
The fifth step is the processing phase where the server and the client communicate via the Send and Recv methods (transfer data).
If the send succeeds, the buffer already has the data you sent.
When #调用recv, the server must specify an integer that corresponds to the maximum amount of data that can be received through this method call. The Recv method enters the "blocked" state when it receives the data, and finally returns a string that represents the received data. If the amount of data sent exceeds the allowable recv, the data will be truncated. The excess data will be buffered on the receiving side. When Recv is called later, the extra data is removed from the buffer (and any other data that the customer may have sent since the last call to Recv).
Connection.settimeout (5) buf = CONNECTION.RECV (1024)
The specific treatment is as follows:
if buf = = ' 1 ': connection.send (' Welcome to Python server! ') else: connection.send (' please go out! ')
Send data to the client using send. The client uses recv to receive data.
Import Socketsock = Socket.socket (socket.af_inet, socket. Sock_stream) sock.connect ((' localhost ', 7556)) import Timetime.sleep (2) sock.send (' 1 ') print SOCK.RECV (1024) Sock.close ()
This is the whole communication process.
The full code is as follows:
Server side:
#!/usr/bin/env pythonimport Socketsock=socket.socket (socket.af_inet,socket. Sock_stream) sock.bind ((' localhost ', 7556)) Sock.listen (5) while True: connection,address = sock.accept () Print "Client IP is" print address try: connection.settimeout (5) buf = Connection.recv (1024x768) if BUF = = ' 1 ': connection.send (' Welcome to Python server! ') else: connection.send (' please go out! ') Except socket.timeout: print ' Time Out ' connection.close ()
The client side is as follows:
#!/usr/bin/env Pythonimport socketsock = Socket.socket (socket.af_inet, socket. Sock_stream) sock.connect ((' localhost ', 7556)) import Timetime.sleep (2) sock.send (' 1 ') print SOCK.RECV (1024) Sock.close ()
Remember to close the socket when it is finished. "The above code, the server side forgot to close the socket."
Of course, in the socket connection process, it is best to use try except to do error handling.
Note that both the Accept function and the recv function are blocking. That is, they are waiting until a client is connected or the latter has data to receive.
Here is a small example of FTP.
Multiple threads are used to process each request.
Sample code Click here to download this site.
Established if the transmission path does not exist.
Hopefully this article will help you with Python programming.