Discussion:
[tornado] My Tornado server cannot be accessed by other devices in the local network. Where did I do wrong?
Akmal Hakimi
2018-09-07 04:44:55 UTC
Permalink
So this is my main code that will randomly push the data to the tornado web
server:

import time
from random import randint
from web_app import WebAppModule

# Start webapp
web_app_client = WebAppModule()
web_app_client.initialize_web_app_client(web_app_client)

def random_int(max):
return (randint(0, max))

def send_message():

name_arr = ["John Doe","Danial Johnson","Zack
Smith","Cheryl","Jeremy Chong","Sivakumar","David Gilbert","Ted Stinson"]
id_arr =
["018359","018332","015421","014656","015114","017712","014651","015892"]
category_arr =
["Admin","Staff","Staff","Staff","Staff","Staff","Staff","VIP"]
preference_arr = ["Milk Tea","White Coffee","Green Tea","Black
Coffee","Mocha Coffee","Caramel Coffee","White Coffee","Black Coffee"]

r = random_int(7)
print name_arr[r] + " detected..."

web_app_client.broadcast_message(name_arr[r],id_arr[r],category_arr[r],preference_arr[r])

if __name__ == "__main__":

print "Starting web app..."
time.sleep(5)
while True:
send_message()
time.sleep(2)

cam.release()

And this is the code for the Tornado server:

#!/usr/bin/python
# -*- encoding: UTF-8 -*-

import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
import threading
import os
import logging
clients = []
wsThread = None

web_app_client = None

class WebAppModule():
def __init__(self):
global wsThread
if wsThread==None:
wsThread = threading.Thread(target=start_websocket).start()

def initialize_web_app_client(self,client):
global web_app_client
web_app_client = client

def
broadcast_message(self,username="N/A",userid="N/A",category="N/A",preference="N/A"):
for client in clients:
if imageurl=="":
imageurl='{{ static_url("face/default.jpg") }}'
msg = {'username': username, 'userid': userid,'category':
category, 'preference': preference,}
global clients

client.write_message(msg)
self.process_running = False
print "Execution paused..."
while self.process_running==False:
pass
print "Execution resumed..."

def start_websocket():
application.listen(9000)
tornado.ioloop.IOLoop.instance().start()

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('web/index.html')

class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
clients.append(self)
print 'web app connection opened...'

def check_origin(self, origin):
return True

def on_message(self, message):
print 'received:', message
if "DONE" in message:
web_app_client.process_running = True

def on_close(self):
clients.remove(self)
print 'connection closed...'

settings = {
"static_path": os.path.join(os.path.dirname(__file__), "web"),
}

application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
], **settings)


From what I understand, sending the message to all client via "for client
in clients" should make it work. However, it didn't for my case. Where did
I do wrong?

Whenever I try connecting from other devices, the console is showing
"Websocket connection to 'ws://localhost:9000/ws' failed. Error in
connection establishment: net::ERR_CONNECTION_REFUSED"
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ben Darnell
2018-09-08 23:17:57 UTC
Permalink
Tornado is not designed to be used with multiple threads. You must call all
Tornado methods such as write_message() from the IOLoop thread. Use
IOLoop.current().add_callback() to transfer control to the IOLoop thread.
See http://www.tornadoweb.org/en/stable/#threads-and-wsgi for more.

-Ben
Post by Akmal Hakimi
So this is my main code that will randomly push the data to the tornado
import time
from random import randint
from web_app import WebAppModule
# Start webapp
web_app_client = WebAppModule()
web_app_client.initialize_web_app_client(web_app_client)
return (randint(0, max))
name_arr = ["John Doe","Danial Johnson","Zack
Smith","Cheryl","Jeremy Chong","Sivakumar","David Gilbert","Ted Stinson"]
id_arr =
["018359","018332","015421","014656","015114","017712","014651","015892"]
category_arr =
["Admin","Staff","Staff","Staff","Staff","Staff","Staff","VIP"]
preference_arr = ["Milk Tea","White Coffee","Green Tea","Black
Coffee","Mocha Coffee","Caramel Coffee","White Coffee","Black Coffee"]
r = random_int(7)
print name_arr[r] + " detected..."
web_app_client.broadcast_message(name_arr[r],id_arr[r],category_arr[r],preference_arr[r])
print "Starting web app..."
time.sleep(5)
send_message()
time.sleep(2)
cam.release()
#!/usr/bin/python
# -*- encoding: UTF-8 -*-
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
import time
import threading
import os
import logging
clients = []
wsThread = None
web_app_client = None
global wsThread
wsThread = threading.Thread(target=start_websocket).start()
global web_app_client
web_app_client = client
def
imageurl='{{ static_url("face/default.jpg") }}'
category, 'preference': preference,}
global clients
client.write_message(msg)
self.process_running = False
print "Execution paused..."
pass
print "Execution resumed..."
application.listen(9000)
tornado.ioloop.IOLoop.instance().start()
self.render('web/index.html')
clients.append(self)
print 'web app connection opened...'
return True
print 'received:', message
web_app_client.process_running = True
clients.remove(self)
print 'connection closed...'
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "web"),
}
application = tornado.web.Application([
(r'/ws', WSHandler),
(r'/', MainHandler),
], **settings)
From what I understand, sending the message to all client via "for client
in clients" should make it work. However, it didn't for my case. Where did
I do wrong?
Whenever I try connecting from other devices, the console is showing
"Websocket connection to 'ws://localhost:9000/ws' failed. Error in
connection establishment: net::ERR_CONNECTION_REFUSED"
--
You received this message because you are subscribed to the Google Groups
"Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for '[tornado] My Tornado server cannot be accessed by other devices in the local network. Where did I do wrong?' (Questions and Answers)
28
replies
Is it a mistake to eliminate all landline telephones ??
started 2020-08-06 17:45:42 UTC
land phones
Loading...