Python socket programming
It takes six steps to establish a server connection.
The first step is to create a socket object. Call the socket constructor.
Socket=socket.socket (familly,type)
The value of family can be AF_UNIX (UNIX domain for interprocess communication on the same machine), AF_INET (for TCP and UDP of IPV4 protocol), as for the type parameter, SOCK_STREAM (stream socket) or SOCK_DGRAM (Datagram socket), SOCK_RAW (raw socket).
The second step is to bind (assign) the socket to the specified address, socket.bind (address)
Address must be a two-element tuple, ((host,port)), hostname or ip address + port number. If the port number is in use or reserved, or if the hostname or ip address is incorrect, a socke.error exception is thrown.
Step 3, after binding, you must have the socket ready to accept the connection request.
Socket.listen (backlog)
Backlog specifies a maximum number of connections, at least 1, which must be queued when connection requests are received and rejected if the queue is full.
In step 4, the server socket waits for the customer to request a connection through the accept method of socket:
Connection,address=socket.accept ()
When the accept method is called, the socket enters the 'waiting' (or blocking) state. When the client requests a connection, the method establishes the connection and returns to the server. The accept method returns a tuple with two elements, such as (connection,address). The first element (connection) is the new socket object through which the server communicates with the customer; the second element (address) is the customer's internet address.
Step 5 is the processing stage.
The server and the client communicate (transfer data) through send and recv methods. The server calls send and sends information to the customer as a string. The send method returns the number of characters sent. The server uses the recv method to receive information from the customer. When calling recv, you must specify an integer to control the maximum amount of data accepted for this call. The recv method enters the 'blocket' state when it accepts the data, and finally returns a string that represents the received data. If the amount sent exceeds the amount allowed by recv, the data will be truncated. The excess data will be buffered at the receiving end. When recv is called later, the excess data is removed from the buffer.
Step 6, the transfer ends, and the server calls the close method of socket to close the connection.
It takes four steps to establish a simple customer connection.
Step 1, create a socket to connect to the server socket=socket.socket (family,type)
Step 2, use the connect method of socket to connect to the server socket.connect ((host,port))
In step 3, the client and server communicate through the send and recv methods.
Step 4, when finished, the customer closes the connection by calling the close method of socket.
Server.py
#! / usr/bin/env python
# TCP-server
Import socket
Import time
HOST=''
PORT=8001
BUFSIZ=1024
Sock=socket.socket (socket.AF_INET,socket.SOCK_STREAM)
Sock.bind ((HOST,PORT))
Sock.listen (5)
While True:
Print 'waiting for connection...'
Connection,address=sock.accept ()
Print ('connection from', address)
While True:
Try:
Data=connection.recv (BUFSIZ)
Except:
Print (e)
Connection.close ()
Break
If not data:
Break
Time.ctime (), data.decode ('utf8'): [% s]% s'%
Connection.send (s.encode ('utf8'))
Print ([time.ctime ()],':', data.decode ('utf8'))
Connection.close ()
Sock.close ()
Client.py
#! / usr/bin/env python
# TCP-client
Import socket
Import sys
HOST='127.0.0.1'
PORT=8001
BUFSIZ=1024
Client=socket.socket (socket.AF_INET,socket.SOCK_STREAM)
Client.connect ((HOST,PORT))
While True:
Data=raw_input ('-->')
If data=='quit':
Sys.exit ()
Client.send ('% s\ r\ n'% data.encode ('utf8'))
D=client.recv (BUFSIZ)
Print (d.decode ('utf8'))