SoFunction
Updated on 2024-10-30

Python Network Programming Explained

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

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

3. Before the server responds to the client, it first creates a communication node which enables the server to listen for requests.

I. Sockets: communication endpoints

1. Sockets

Sockets are computer network data structures that embody the concept of "communication endpoints" described in the previous section. Before any type of communication can begin, a network application must create a socket.

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-allowing some infrastructure for communication-then a hostname and port number are like a combination of an area code and a phone number. Valid port numbers range from 0-65535 (port numbers less than 1024 are reserved for the system)

3. Connection-oriented sockets and sockets with no connections

Connection-oriented, a connection must be established before communication can take place. The main protocol that implements this type of connection is TCP (Transmission Control Protocol)

Connectionless, no connection needs to be established before communication. The main protocol is UDP (User Datagram Protocol)

II. Network Programming in Python

1. socket() module function

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

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

2. Common socket object methods and properties

name (of a thing) descriptive
Server socket methods  
()  Bind addresses (hostname, port number pairs) to sockets
() Setting up and starting the TCP listener
() Passively accepts TCP client connections and waits until the connection arrives (blocking)
Client Socket Methods  
() Initiate a TCP server connection
s.connect_ex() The extended version of connect returns the problem as an error code instead of throwing an exception.
Common socket methods  
() Accepting TCP messages
() Send TCP message
() Send TCP messages in full
() Receiving UDP messages
() Close connection
() Close Sockets

3、Create TCP server

ss = socket()  # Create server sockets
()      # Sockets bound to addresses
()      #Listening for connections
inf_loop:       #Server infinite loop
  cs = ()     # Receive client connections
  comm_loop:        # Communication loop
    ()/()  # Dialog (receive, send)
  ()       # Close client sockets
()        #Shutting down server sockets(selectable)
#!/usr/bin/env python
# TCP timestamp server
from socket import *
from time import ctime
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET,SOCK_STREAM)
(ADDR)
(5)
while True:
  print('waiting for connecting...')
  tcpClisock, addr = ()
  print('...connected from:',addr)
  while True:
    data = (BUFSIZ)
    if not data:
      break
    ('[%s] %s' % (bytes(ctime(),'utf-8'),data))
  ()
()

4、Create TCP client

cs = socket()
()
comm_loop:
  ()/()
()
#!/usr/bin/env python
#TCP timestamp client
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpClisock = socket(AF_INET,SOCK_STREAM)
(ADDR)
while True:
  data = input('> ')
  if not data:
    break
  (data)
  data = (BUFSIZ)
  if not data:
    break
  print(('utf-8'))
()

5. Create UDP server

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

6. Create UDP client

cs = socket()
comm_loop:
  ()/()
()
#!/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
  (data,ADDR)
  data, ADDR=(BUFSIZ)
  if not data:
    break
  print(data)
()

This is the whole content of this article about Python network programming in detail, I hope it will be helpful to you. Welcome to refer to:Python enumerate function code analysisPython data type judgment type and isinstance difference example analysisThe beauty of the requests library session object in python.Wait, feel free to leave a comment if you have any questions and I'll change them if there are any.