Python Socket Communication Time Over Processing

Asked 2 years ago, Updated 2 years ago, 34 views

I would like to move on to the next processing when I receive data from the processing server, and I would like to move on to the next processing in the same way if I do not receive data sent by clicking for about 20 seconds.

If I flag it over time, it will be flagged later or I thought about turning it around with a while statement, but I didn't know how to get out of it when I received it.

source code:

import subprocess
import time
import socket
from threading import Thread

host='127.0.0.1'
port = 50007
Work=""

client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)#Create Object
client.connect(host,port))# Connect to server
client.send("test".encode('utf-8'))#Send appropriate data

def Get_click():
    response_d=client.recv(4096)
    print(response_d)
    response=response_d.decode('utf-8')
    # The data you receive is t01/t02/t03
    return response

time.sleep(2)
print ("start")
data=Get_click()

if data.find("t01")>=0:
    print(1)
elif data.find("t02")>=0:
    print(2)
elif data.find("t03")>=0: 
    print(3)
else: # I want to be assigned here if I don't hear from you
    print("error")

python python3

2022-09-29 20:43

1 Answers

socket.settimeout to set the timeout period,
How about that?

# You must call before connect.

Add

If you time out,
by invalidating the Get_Click() return value. else conditions can be processed.

# Example: Returns an empty value after timeout.

def Get_click():
    try:
        response_d=client.recv(4096)
    except socket.timeout:
        response_d='"
    print(response_d)
    response=response_d.decode('utf-8')
    # The data you receive is t01/t02/t03
    return response


2022-09-29 20:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.