directory

Network programming

TCP programming

Information transfer between the client and the server

UDP programming

Principle of multitasking

Starting a process pool

Interprocess communication


Network programming

Network programming Overview:

  • Since the birth of the Internet, now basically all programs are network programs, few stand-alone version of the program
  • A computer network is a way of connecting computers together so that they can communicate with each other. Network programming is how to realize the communication between two computers in the program
  • Network programming with Python is to communicate with the communication ports of other server processes within the Python process itself

 

TCP programming

TCP is about establishing reliable links, and both parties can communicate and send data in the form of streams.

Client: When creating a TCP connection, the client that initiates the connection is called client. Server: Receives the connection from the clientCopy the code

Socket () : Creates a socket

Bind () : indicates the bind port number

Listen () : is responsible for listening, can pass a parameter

Accept () : Wait for connection

The connect () : the connection

Write () : After the connection is established, the client can write data to the server

Read () : Thinking about data

 

Example: Visit sina.com client

The socket library contains everything for network programming
import socket

Create a socket
Parameter 1: specifies the protocol AF_INET or AF_INET6
SOCK_STREAM execution uses the stream-oriented TCP protocol
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#2. Establish a connection
Parameter: is a tuple, the first element is the IP address of the server to connect to, the second parameter is the port
sk.connect(('www.sina.com'.80))

sk.send(B 'get/HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')

Wait to receive data
data = []
while True:
    Receive 1K data at a time
    tempdata = sk.recv(1024)
    if tempdata:
        data.append(tempdata)
    else:
        break

dataStr = (b''.join(data)).decode('utf-8')

# Disconnect
sk.close()
print(dataStr)
Copy the code

Result: Access failed, return status code 302: Request data found at another address

 

Information transfer between the client and the server

Go to CMD to get your own IP address

Then create two PY files, one for the client and one for the server

Client code:

Import socket client = socket.socket(socket.af_inet, socket.sock_stream) client.connect((' your IP address ',8081)) count = 0 while True: Encode ('utf-8')) info = client.recv(1024) print(' utf-8') ',info.decode('utf-8'))Copy the code

Server-side code:

Import socket server = socket.socket(socket.af_inet, socket.sock_stream) # bind IP port server.bind((' your IP address ',8081)) # listen Server. Listen (# 5) wait for link clientSocket, clientAddress = server. The accept () print (' server startup success... ') # print(str(clientSocket),clientAddress) while True: Recv (1024) print(' data: '+data.decode('utf-8'))Copy the code

The server side is then run at runtime, followed by the client side

If you run the client first, an error will be reported and the server cannot be found

Run result: First is server-side

 

UDP programming

In contrast to TCP, UDP is a connectionless protocol. Using UDP, you do not need to establish a link, but only need to know the IP address and port number of the peer party

Although UDP is unreliable, it is faster than TCP and can be used to transmit data with low requirements

Udp = socket. Socket (socket.AF_INET,socket. udp.send('wahtaijvajvi'.encode('utf-8'))Copy the code

 

Principle of multitasking

Modern operating systems (Windows, Linux, Mac OS X, Unix, etc.) all support multitasking multitasking: An operating system can run multiple tasks simultaneously

Early computers are mainly single-core CPU single-core CPU to achieve multi-task principle: the operating system takes turns to execute each task alternately, QQ executes 2US (microseconds), switches to wechat, then executes 2US, then switches to Momo, and then… On the surface, each task is executed repeatedly, but CPU scheduling is too fast. As a result, it feels like everything is happening at the same time.

True parallel multitasking can only be done on a multi-core CPU, but since there are many more tasks than there are cpus, the operating system automatically schedules many tasks to each core in turn for concurrent execution: seemingly executed together, the number of tasks is greater than the number of CPU cores for parallel execution: When executed together, the number of tasks is less than the number of CPU cores

Ways to achieve multitasking:

  1. Multi-process mode
  2. Multithreaded mode
  3. Coroutines mode
  4. Multi-process + multi-thread mode
Multiprocessing: a cross-platform version of the multiprocess module that provides a Process class to represent a Process objectCopy the code

Example:

from multiprocessing import Process
import time

def run() :
    while True:
        print('zk is a gay... ')
        time.sleep(1.5)

if __name__=='__main__':
    print('Main process started')
    Create a child process
    #target indicates the task performed by the process
    p = Process(target=run)
    # start process
    p.start()
    The main process is running
    while True:
        print('what happen... ')
        time.sleep(1)
Copy the code

Running results:

Os.getpid () obtains the CURRENT process ID. Os.getppid () obtains the ID of the parent process of the current processCopy the code

Example:

from multiprocessing import Process
import time
import os

def run() :
    while True:
        #os.getpid() gets the current process ID
        # os.getppId () gets the parent process ID of the current process
        print('zk is a gay... '+str(os.getpid())+'Parent ID:'+str(os.getppid()))
        time.sleep(1.5)

if __name__=='__main__':
    print('Main process started'+str(os.getpid()))
    Create a child process
    #target indicates the task performed by the process
    p = Process(target=run)
    # start process
    p.start()
    The main process is running
    while True:
        print('what happen... '+str(os.getpid()))
        time.sleep(1)
Copy the code

Running results:

Order of parent and child: Termination of the parent process does not affect the child process

Global variables cannot be shared across multiple processes

Example: The sibling process changes one global variable and leaves the other unchanged

n = 100
def run() :
    global n
    n += 1
    #os.getpid() gets the current process ID
    # os.getppId () gets the parent process ID of the current process
    print('zk is a gay... '+str(n))

def fun() :
    global n
    print(n)

if __name__=='__main__':
    p = Process(target=run)
    p.start()
    k = Process(target=fun)
    k.start()
Copy the code

Running results:

 

Starting a process pool

Use process pooling methods to call a large number of child processes

from multiprocessing import Pool
import time
import os,random


def run(num) :
    print('Child process %d starts -- %s' % (num,os.getpid()))
    start = time.time()
    # Random pause time
    time.sleep(random.choice([1.2.3]))
    end = time.time()
    print('Child process %d ends -- %s, time: %d' % (num,os.getpid(),end-start))


if __name__=='__main__':
    print('Parent process started')

    Create multiple processes
    The number of processes that can run concurrently. The default size is the number of CPU cores
    pc = Pool(3)
    for i in range(5) :Add a process to the process pool for unified management
        pc.apply_async(run,args=(i,))
    You must call close before calling join. You cannot add new processes after calling close
    pc.close()
    The join object waits for all child processes in the pool to complete before executing the parent process
    pc.join()

    print('Parent process terminated')
Copy the code

Running results:

 

Interprocess communication

Use queue to realize data transmission between two processes

Child process 1 — queue — Child process 2

Child process 1 — queue — Child process 2

To communicate between processes, the parent process needs to create a queue and pass it to the child process

Example:

from multiprocessing import Queue,Process
import os,time
def write(q) :
    print('Start write child process %s' % os.getpid())
    for chr in ['A'.'B'.'C'.'D']:
        q.put(chr)
        time.sleep(1)
    print('End write child process %s' % os.getpid())

def read(q) :
    print('Start reading child process %s' % os.getpid())
    while True:
        value = q.get(True)
        print('value:',value)
    print('End reading child process %s' % os.getpid())

if __name__=='__main__':
    The parent process needs to create a queue and pass it to the child
    q = Queue()
    pw = Process(target=write,args=(q,))
    pr = Process(target=read, args=(q,))

    pw.start()
    pr.start()
    pw.join()
    Terminate (); if the write process terminates, the read process terminates
    pr.terminate()
    print('Parent process terminated')
Copy the code

Result: because the read process is in an infinite loop, it can only be forcibly terminated

 

 

 

 

Learn together and make progress together. If there are any mistakes, please comment