Python Foundation-40-Socket Case 2

Preface

Based on the demo from the previous article, this article adds some functional requirements

Server: Still send 30 random numbers between 1 and 30, and show the results returned by the client

Client:

1) The first client calculates parity and returns the result

2) The second client calculates twice the number and returns the result

3) The third client determines if the number is greater than 10 and returns the result

 

Code section

server side

# coding:utf-8
import socket
import random
import time

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(("",8000))
sock.listen(5)
# 30 numbers randomly generated between 1 and 30
random_num = [random.randint(1,30) for i in range(30)]

def send_data(content,address):
    print("%s:%s Connecting"%address)
    for num in random_num:
        # Delay here to avoid data clutter
        time.sleep(1)
        # Server sends data
        content.send("%d".encode()%num)
        # Server Receives Data
        recv = content.recv(512).decode()
        print(recv)

while True:
    content, address = sock.accept()
    send_data(content,address)

sock.close()

client end one (calculates odd and even numbers)

# coding:utf-8
import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(("127.0.0.1",8000))

def recv_data():
    recv = sock.recv(512).decode()
    rec = int(recv)
    if rec % 2 == 0:
        print("%d Is Even"%rec)
        sock.send("%d Is Even".encode()%rec)
    else:
        print("%d is odd" % rec)
        sock.send("%d is odd".encode()%rec)

while True:
    try:
        recv_data()   # Direct execution will result in an error at the end. Error information will be ignored here
    except Exception as e:
        pass

sock.close()

 

client end two (double value calculated)

# coding:utf-8
import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(("127.0.0.1",8000))

def recv_data():
    recv = sock.recv(512).decode()
    rec = int(recv)
    res = rec*2
    print("%d Twice as many as%d"%(rec,res))
    sock.send("%d Twice as many as%d".encode()%(rec,res))

while True:
    try:
        recv_data()
    except Exception as e:
        pass

sock.close()

 

client end three (judgement greater than 10)

# coding:utf-8
import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect(("127.0.0.1",8000))

def recv_data():
    recv = sock.recv(512).decode()
    rec = int(recv)
    if rec > 10:
        print("%d More than 10"%rec)
        sock.send("%d More than 10".encode()%rec)
    else:
        print("%d Less than 10"%rec)
        sock.send("%d Less than 10".encode()%rec)

while True:
    try:
        recv_data()
    except Exception as e:
        pass

sock.close()

 

Function

After starting the service side, run client1, client2, client3 in turn

 

optimization

It's possible to do what we want, but the server side is one that handles client requests, but we want the three computing functions to go on and end at the same time

The result is returned to the server.

This allows the server to open multiple threads to interact with the client at the same time, saving running time and improving efficiency.

 

Modify server-side code, client unchanged

# coding:utf-8
import socket
import random
import time
import threading

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(("",8000))
sock.listen(5)
# 30 numbers randomly generated between 1 and 30
random_num = [random.randint(1,30) for i in range(30)]

def send_data(content,address):
    print("%s:%s Connecting"%address)
    for num in random_num:
        # Delay here to avoid data clutter
        time.sleep(1)
        # Server sends data
        content.send("%d".encode()%num)
        # Server Receives Data
        recv = content.recv(512).decode()
        print(recv)

while True:
    content, address = sock.accept()
    t = threading.Thread(target=send_data,args=(content,address))
    t.start()

sock.close()

 

Multithreaded code execution

 

Keywords: socket less

Added by 10legit10quit on Thu, 07 Nov 2019 17:47:39 +0200