Reputation: 12888
Being completely new to web sockets and relatively new to Python, I was wondering if it is possible to write a server (or if one already exists, that would be even better) in Python that receives data via a standard socket (UDP) and forwards that data via web sockets to a browser? I notice in using Tornado, that the last line of your main is typically:
tornado.ioloop.IOLoop.instance().start()
Which creates a "listener" loop and would seem to prevent me from receiving any data on my standard socket. Is it possible to do this?
Upvotes: 0
Views: 1008
Reputation: 22154
Tornado doesn't have any explicit APIs for dealing with UDP, but you can add a UDP socket with IOLoop.add_handler
(the following code is untested but should give you the basic idea):
def handle_udp(sock, events):
while True:
try:
data, addr = sock.recvfrom(bufsize)
# do stuff with data
except socket.error as e:
if e.errno in (errno.EAGAIN, errno.WOULDBLOCK):
# nothing more to read, return to the IOLoop
return
sock = bind_udp_socket()
sock.setblocking(0)
IOLoop.current().add_handler(sock, IOLoop.READ)
IOLoop.current().start()
Upvotes: 1