Python socket network programming TCP and IP servers communicate with clients
- 2020-05-19 05:09:51
- OfStack
Python socket network programming
Beginners python, recently bought two books "python programming from entry to practice" the 3rd edition Python core programming, first book is primarily concerned with the use of some basic grammar and 1 some basic method, while the second is much deeper, his opinion is 1 and half know solution, just see this part of the network programming, there are still many don't understand of place, but want to come through oneself constantly, constantly looking for learning, should become fully in the morning and evening...
The main module used in this part is the socket module. In this module, you can find the socket() function, which is used to create socket objects. Sockets also have their own set of methods, which can realize network communication based on sockets.
socket() module function
To create a socket, you must use the socket.socket () function, which has the following 1-like syntax
socket(socket_family, socket_type, protocol=0)
Where socket_family is AF_UNIX or AF_INET,socket_type is SOCK_STREAM or SOCK_DGRAM, protocol is usually omitted, default =0.
So to create an TCP/IP socket, you can call socket.socket () in the following manner.
tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Also create the UDP/IP socket using the method call below
udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Create the TCP server
The process for creating an TCP server is as follows, not the actual code
ss = socket() # Create a server socket
ss.bind() # A socket is bound to an address
ss.listen() # Listening to the connection
inf_loop: # Server infinite loop
cs = ss.accepr() # Accept client connections
comm_loop: # Communication cycle
cs.recv()/cs.send() # Dialogue (receive / Send)
cs.close() # Close the client socket
ss.close() # Close the server socket (optional)
In practice, the basic process of creating TCP server is as mentioned above, which may be slightly different, but the basic idea should be the same. Here is the code of the real server:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from socket import * # will socket Attributes are introduced into the namespace
HOST = '' # This is the bind() The identifier of the method indicates that any available address can be used
PORT = 21571 # The port number
BUFSIZ = 1024 # Buffer size, 1kb
ADDR = (HOST,PORT) # Address??
tcpSerSocket = socket(AF_INET,SOCK_STREAM) # create tcp The socket
tcpSerSocket.bind(ADDR) # Binds the address to the socket
tcpSerSocket.listen(5) # Sets and starts socket listening
while True: # Loop indefinitely, waiting for the client to connect
print('waiting for connection...')
tcpCliSocket,addr = tcpSerSocket.accept() # Passively accept client connections
print('...connected from:',addr)
while True: # The dialog loops, waiting for the client to send the message
data = tcpCliSocket.recv(BUFSIZ) # Receive client messages
if not data: # If the message is blank, jump out of the dialog loop and close the current connection
break
tcpCliSocket.send(data) # If a message is received, it is returned to the client intact
tcpCliSocket.close()
tcpSerSocket.close()
Create the TCP client
As in 1 above, a simple non-code flow
cs = socket() # Create a client socket
cs.connect() # Trying to connect to the server
comm_loop: # Communication cycle
cs.send()/cs.recv() # Conversation (send / Receive)
cs.close() # Close the client socket
Actually creating a client is also a step above translation
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from socket import *
HOST = 'localhost' # The hostname of the server
PORT = 21571 # The port number
BUFSIZ = 1024 # The buffer
ADDR = (HOST,PORT) # address
tcpCliSocket = socket(AF_INET,SOCK_STREAM) # Create a client socket
tcpCliSocket.connect(ADDR) # Connect to server
while True: # Communication cycle
data = input('> ') # The client enters the information
if not data: # If the input information is empty, the loop is broken and the communication is turned off
break
data = str.encode(data)
tcpCliSocket.send(data) # Send client information
data = tcpCliSocket.recv(BUFSIZ) # Accept the information returned by the server
if not data: # If the server does not return information, close the communication loop
break
print('get:',data.decode('utf-8'))
tcpCliSocket.close()
Of course, this is only the most basic communication, and about the host name, port number and other things for the time being not very well understood, at present what is done is only on the same computer communication, port number also need 1, if different computers to communicate what to do? I'm just a little white...
Thank you for reading, I hope to help you, thank you for your support of this site!