[英]Python socket sends a message to the Client after no data received in 5 seconds
python 套接字發送的一種方式,例如:如果客戶端在 5 秒內沒有發送任何內容,則向客戶端發送“超時”,然后關閉連接(我可以關閉連接,這將是 c.close() 但那據我所知在這種情況下)? 當涉及到像這樣的更高級的套接字編程時,我是一個初學者。 我不知道該嘗試什么,所以無法發送“我嘗試過的”代碼。 我所能發送的只是我當前的服務器代碼。
import socket
from random import *
s = socket.socket()
s.bind(("localhost",int(input("Port\n>>> "))))
s.listen(1)
while True:
c,a = s.accept()
data = c.recv(1024)
#Somewhere here, it would wait for 5 sec for a message to come and if not then:
c.send("Timeout".encode())
c.close()
只是設置超時?
s.settimeout(5.0)
data = c.recv(1024)
if(not data):
c.send(b"Timeout")
c.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.