简体   繁体   English

python中的socket.recv(255)挂起等待数据

[英]socket.recv(255) in python hung on waiting for data

Newbie question here, 新手问题在这里,

I am trying to hack an existing python program that searches for bluethooth signal 我正在尝试破解现有的搜索bluethooth信号的python程序

all works fine if there is a bluetooh transmitter around, 如果周围有蓝牙发射器,则一切正常,

but the program simply sits there if there is no Bluetooth signal around. 但是如果周围没有蓝牙信号,则程序只会坐在那里。

I found out that it is getting hung on this line 我发现它挂在这条线上

    pkt = sock.recv(255)

I am naively guessing that is simply sitting there waiting for data, I want it to give me an error or timeout after lets say 10 seconds. 我天真地猜测这只是坐在那里等待数据,我希望它在让我说10秒钟后给我一个错误或超时。

How do I do this? 我该怎么做呢? Is my thinking correct? 我的想法正确吗?

Thanks 谢谢

Call settimeout before recving. 接收前调用settimeout Then it will raise an error if it takes too long. 如果花费的时间太长,则会引发错误。

sock.settimeout(10)
try:
    pkt = sock.recv(255)
except socket.error:
    print "connection timed out!"
    return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM