我正在提拉订阅UPnP设备(WeMo运动传感器)上的事件。我首先向设备发送一个HTTP订阅请求,设备应该开始在指定地址向我发送事件通知。这个部分工作得很好(除了我收到了太多的通知;即使状态没有改变,但是对于不同的线程来说这是一个不同的问题)
如果我在一个单独的python进程上运行keepListening函数,那么一切正常。但是,当我将函数作为线程运行时,它不起作用
import socket
import requests
from threading import Thread
def keepListening(): #running this function on a separate process works
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(600)
sock.bind(('192.168.10.231',1234))
sock.listen(5)
while 1:
notification = ''
try:
conn, addr = sock.accept()
conn.setblocking(1)
notification= conn.recv(1024)
conn.sendall(r'''HTTP/1.1 200 OK
Content-Type: text/plain
''')
except Exception as er:
print er
print notification
x = Thread(target=keepListening)
x.start()
message = {
'CALLBACK': '<http://192.168.10.231:1234>',
'NT': 'upnp:event',
'TIMEOUT': 'Second-600',
'HOST': '192.168.10.159:49153'}
k = requests.request('SUBSCRIBE','http://192.168.10.159:49153/upnp/event/basicevent1',headers=message)
print k
# keep doing other important works
每一个事件通知都必须回复200个OK回复,否则设备将不会再发送通知;这是我很难理解的事实。我有一个疑问,这可能很愚蠢,那就是,当在线程中运行时,与单独的进程相反,应答消息没有被及时发送,因此设备不再发送任何通知。 值得一提的是,即使我在线程中运行函数,我也确实会在订阅后得到初始通知(根据UPnP协议,设备必须在订阅后立即强制发送初始通知),但我没有收到进一步的通知(这表明我的200ok回复没有正常通过;不过,我在wireshark上看到了)
在线程中运行函数(与单独的进程相反)会导致函数失败,有什么不同吗?在
谢谢。在
我假设,现在的情况是,在线程激活并开始监听接口之前,您发送了订阅请求。所以设备无法连接到插座。在
几天前,我得到了一个wemo运动传感器,开关和树莓,所以我开始修补。在
脚本订阅wemo设备的“binaryState”事件。 每次事件发生时,它都会打印出一个“警报”(你可以在那里做其他事情)。 250秒后,它将续订。在
要使脚本适应您的需要,您必须更改IP:
你的电脑
远程ip:wemo传感器或交换机的ip
我是python新手(3天前开始的),所以脚本可能需要一些修改,但它可以工作。在
相关问题 更多 >
编程相关推荐