FAQ Database Discussion Community


Converting web.asynchronous code to gen.coroutine in tornado

python,python-2.7,tornado
I want to convert my current tornado app from using @web.asynchronous to @gen.coroutine. My asynchronous callback is called when a particular variable change happens on an IOLoop iteration. The current example in Tornado docs solves an I/O problem but in my case its the variable that I am interested in....

how to show non ASCII character using python [closed]

python,tornado
I want to see the original value(in a language other than English) in console(not in a unicode format) without print function. But if I use that code without print function then showing anonymous value(in unicode). I am using tornado framework...

Tornado Create a Secure Cookie with expiry time less than 1 day

python,cookies,tornado
I'm using Tornado's Secure Cookie functionality to implement user session. The set_secure_cookie function enables us to create a Cookie where lower limit for cookie expiry can be set to 1 day. Now, I need to expire these secure cookies in one hour. Can I achieve this easily? One solution is...

tornado.web.stream_request_body: _xsrf missing error even with _xsrf input within html

python,tornado
Utilizing the Tornado library within Python I have come across a very unusual error. It seems that when I have decorated my file upload handler with '@tornado.web.stream_request_body' the webserver throws the error: WARNING:tornado.general:403 POST /upload (ip-address): '_xsrf' argument missing from POST WARNING:tornado.access:403 POST /upload (ip-address) 1.44ms The code governing the...

Continuously monitor a socket in Tornado Python framework

python,tornado
I am using Tornado and I want to monitor a socket continuously for notifications from a DB server. My application so far looks like this: import functools import tornado import tornado.httpserver from tornado.ioloop import IOLoop class Application(tornado.web.Application): def __init__(self): handlers = [(r"/", MyHandler),] super(Application, self).__init__(handlers) fd = get_socket_file_descriptor() callback =...

python-tornado dont match “/favicon.ico”

python,tornado
question here and here do not help :( I'm trying to handler "/favicon.ico" but it doesn't work as expected.It's wired... My handler looks like: handlers = [ (r'^/$', RootHandler), # this works fine (r'^/favicon\.ico$', IconHandler), # other handlers ] My IconHandler looks like: class IconHandler(tornado.web.RequestHandler): def get(self): self.set_header("Content-Type", "image/vnd.microsoft.icon") with...

SSLV3_ALERT_HANDSHAKE_FAILURE with SNI using Tornado 4.2 in Python 2.9.10

python,python-2.7,ssl,tornado,sni
I have an issue setting the SNI flag correctly using ssl.SSLContext in Python 2.7.10, the handshake fails every time and I can't figure out why. Here is how I tried to do it: import ssl import socket if ssl.HAS_SNI: print "SNI is available" print(ssl.OPENSSL_VERSION) context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.load_cert_chain('cacrt.pem', 'cakey.pem', 'password')...

block.super in Tornado templae system

django,templates,tornado
In Tornado i have a base template with x block similar below: {% block x %} x {% end %} and in second template that inherited from base template i want to mix old value by new value similar django template tag, that is similar below code: {% block x...

Content-Type header not getting set in Tornado

python,tornado
I have the following base class: class CorsHandler(tornado.web.RequestHandler): def set_default_headers(self): super(CorsHandler, self).set_default_headers() self.set_header('Access-Control-Allow-Origin', self.request.headers.get('Origin', '*')) self.set_header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS') self.set_header('Access-Control-Allow-Credentials', 'true') self.set_header('Access-Control-Allow-Headers', ','.join( self.request.headers.get('Access-Control-Request-Headers', '').split(',') +...

Tornado: HTML inside gettext function string

html5,tornado,gettext
I am using gettext for internationalisation. I have HTML tags in one sentence, but after translation the HTML tags are shown as string. msgid "<strong>Dokument(e)</strong> offline verfuegbar machen<br>" msgstr "<strong> Dokument(e)</strong> offline verfügbar machen<br>" Does someone has more experience with this? I need this output: Dokument(e) offline verfügbar machen but...

webrequest object to csv in tornado

python,csv,tornado
I have the following webrequest object body generated by uploading the csv: ------WebKitFormBoundaryZfGb9EY1D9p7BTV9 Content-Disposition: form-data; name="file"; filename="vb_bpm.csv" Content-Type: text/csv 10/05/2015,00:08:17,8,3960,2,52503,19824 10/05/2015,00:19:35,8,3960,2,52542,19824 10/05/2015,02:21:21,8,3960,2,55366,19824 10/05/2015,02:32:11,8,3960,2,55942,19824 10/05/2015,02:43:13,8,3960,2,56139,19824 10/05/2015,02:54:09,8,3960,2,56750,19824 10/05/2015,03:05:08,8,3960,2,57007,19824...

How to insert data from a tornado web-socket to a mySQL database using Django

python,mysql,django,sockets,tornado
I am using Django with mysql to create a monitoring web page. I have also created a web-socket server with tornado. What I want to do is to insert data in mysql database every time I receive something from the web-socket. What kind of connections I should do between Tornado...

Get current user Async in Tornado

python,tornado
When I use get_current_user() I need to check few things in Redis (use tornado-redis) asynchronously. I am doing the following: def authenticated_async(method): @gen.coroutine def wrapper(self, *args, **kwargs): self._auto_finish = False self.current_user = yield gen.Task(self.get_current_user_async) if not self.current_user: self.redirect(self.reverse_url('login')) else: result = method(self, *args, **kwargs) # updates if result is not...

Have Tornado serve static HTML with javascript without using static, public, etc. prefix

python,tornado
Is there a way to avoid prepending "public", "static", etc. to every single javascript src attribute in my HTML files? I'm in the process of converting a basic static server from Node.js to Tornado, and everything has gone smoothly except for this. The equivalent Node.js/Express code I want to emulate...

Can tornado set_secure_cookie expires timestamp?

timestamp,tornado
I check tornado set_secure_cookie function code, it can set cookie in expires_days, but I want to know whether it can be set expires in timestamp form. For example: self.set_secure_cookie("cookie_name", "cookie_value", expires=timestamp) timestamp is value like 1432527031. Could somebody answer me?...

Issue with multiple records of the same subdocument returned for MongoDb and Python

python,mongodb,tornado,pymongo
I'm getting this strange issue when getting back the results of a object with subdocuments from a Mongo database using Python and pymongo. I have a document with a list of sub documents, e.g. User: { "_id": .... hats: [{"colour": "blue" }] } I query this using find_one(). It returns...

Python coroutines on builtin functions

python,tornado,coroutine,python-asyncio,builtin
I'm currently using python 3.4, @coroutine decorator and yield keyword (in tornado) for asynchronous purposes. I wrote an ORM with a lot of objects instrospection which call a "slow" database and override built-in functions like __init__ or __contains__. My question is : For example, when my asynchronous code is in...

Tornado Error Handling

tornado
I want to be able to handle a nicer error that's displayed if i type an incorrect URL E.g. localhost:8000/AFDADSFDKFADS I get an ugly python traceback message because a tornado.web.HTTPError exception is thrown. I know I can use a regex to catch all scenarios other than my proper URLs however...

Tornado ioloop + threading

python,http,tornado,web-frameworks
i have been working on tornado web framework from sometime, but still i didnt understood the ioloop functionality clearly, especially how to use it in multithreading. Is it possible to create separate instance of ioloop for multiple server ??

Writing the Content-Length header to the client from the server in Tornado

python,http,http-headers,tornado
I have a tornado server, that simply prints the headers that the client sent. server.py : import tornado.httpserver import tornado.ioloop import tornado.httputil as hutil def handle_request(request): message = "" try : message = request.headers['Content-Length'] except KeyError : message = request.headers request.connection.write_headers( tornado.httputil.ResponseStartLine('HTTP/1.1', 200, 'OK'), tornado.httputil.HTTPHeaders ({"Content-Length": str(len(message))})) request.connection.finish() print(request.headers) http_server...

How to send data to all clients?

tornado
I have simple server: # -*- coding: utf-8 -*- import os import logging import tornado.httpserver import tornado.ioloop import tornado.options class WSHandler(tornado.websocket.WebSocketHandler): def check_origin(self, origin): return True def open(self): print 'connection opened' def on_message(self, message): self.write_message("Echo: " + message) print 'received:', message def on_close(self): print 'connection closed' class MainHandler(tornado.web.RequestHandler): def get(self):...

Querying for more than one document inside of view with Motor

python,mongodb,tornado,tornado-motor
The Motor tutorial in the latest doc shows how to get more than one document via tornado.gen.couroutine: @tornado.gen.coroutine def do_find(): cursor = db.test_collection.find({'i': {'$lt': 5}}) for document in (yield cursor.to_list(length=100)): print document It then demonstrates the output synchronously: IOLoop.current().run_sync(do_find) How can I alter this function so that I can call...

How to get to HTTPServer class from RequestHandler?

tornado
I have class based on HTTPServer: class MyServer(tornado.httpserver.HTTPServer): def __init__(self, *args, **kwargs): super(MyServer, self).__init__(*args, **kwargs) self.my_prog = subprocess.Popen( ['python', 'myprog.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE ) and view: class MessageHandler(tornado.web.RequestHandler): def get(self, some_data): # some stuff and # here I would like to get to # MyServer.my_prog I want to send via communicate...

After configure the nginx, I can't see the index page

python,nginx,tornado
This is my first time to use nginx I wrote a simple python/tornado code as the following, when I used python server.py I could see the index.html page. from tornado.options import define, options from db import MongoImpl define("port", default=8000, help="run on the given port", type=int) class IndexHandler(tornado.web.RequestHandler): def get(self): self.render("./pages/index.html")...

Redirecting HTTP to HTTPS on AWS-ELB-Tornado

python,amazon-web-services,amazon-ec2,tornado,aws-ec2
I notice a lot of questions on this topic, but I haven't found any specifically for Tornado. I have a Amazon EC2 Instance running behind a load balancer. The HTTPS cert terminates at the ELB, both port 80/443 are directed to the same Tornado server port. How do I force...

Tornado async basic concepts

python,tornado
I'm trying to understand some concepts of Tornado and async. If we have two sessions X and Y connected to the Tornado server T. Now suppose X performs a database write that is blocking for 10seconds. In that period Y requests something from T. Does T have to wait until...

Tornado sockets using redis sub\pub

python,websocket,redis,tornado
I need advice about performance using web-sockets together with redis pub/sub channels. I use tornado as web-server. I have about 100 clients which listen status of some event. Each client have separate event and need to listen only own event. Socket connection is short-live, something about 1-20 seconds. I see...

Error when running tornado Webserver in terminal

python,tornado
I am trying to run a tornado webserver in the terminal, but when I run it I simply get a space which is completely empty, and then I have no way of closing the server. Right now I am trying the hello world example. import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler):...

How to use Peewee with Tornado perfectly

python,tornado,peewee
I'm useing peewee with my tornado webapp,when I read peewee's document,I found: Adding Request Hooks When building web-applications, it is very important that you manage your database connections correctly. In this section I will describe how to add hooks to your web app to ensure the database connection is handled...

Do I need a queue for a socket pair pattern?

python,sockets,tornado
I'm writing a Tornado server that allows for communication between sockets in a kind of pairing pattern. This looks something like def write_to_paired(self, message): if (paired == online): paired.write_message(message) class typeASocket: def on_message(message): write_to_paired(message) class typeBSocket: def on_message(message): write_to_paired(message) These connections will only ever be 1:1. Do I need a...

Embedding multiple pieces of JavaScript via a Tornado UIModule

javascript,python,tornado
I'm working on a Python package that uses Tornado to send data to the browser for visualization. In order to do this, I want the users to be able to write multiple arbitrary modules for the server to render together on a single page -- including each module's own JavaScript....

Tornado Localisation, set default language

localization,internationalization,locale,tornado
I want to set my default language in my web client to be 'de' when the browser language is 'en' or 'en_US'. I am using tornado localisation. I tried this solution, tornado.locale.set_default_locale('de') but it still shows the web client in english.

python - When are WebSocketHandler and TornadoWebSocketClient completely deleted?

python,object,websocket,tornado,deletion
I'm working on an application that must support client-server connections. In order to do that, I'm using the module of tornado that allows me to create WebSockets. I intend to be always in operation, at least the server-side. So I am very worried about the performance and memory usage of...

Need help on tornado coroutines

python,python-2.7,tornado
I'm new to python and tornado. I was trying some stuff with coroutines. def doStuff(callback): def task(): callback("One Second Later") Timer(1,task).start() @gen.coroutine def routine1(): ans = yield gen.Task(doStuff) raise gen.Return(ans) if __name__ == "__main__": print routine1() I'm trying to get the result of doStuff() function, which I expect to be...

Access header on redirect response with tornado.httpclient.AsyncHTTPClient?

python,cookies,tornado,asynchttpclient
I have a case that I need to extract cookies from a redirect response(login page, for example). I have read the doc, even the source code, but still can't find a way to do this. By default, AsyncHTTPClient will follow the redirect and return the redirected response, which didn't contain...

How to run Flask app with Tornado

python,flask,tornado
I would like to run a simple app written in Flask with Tornado. How do I do this? I want to use Python 2.7 and the latest Tornado version (4.2). from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run() ...

how to debug Tornado async operation

python,asynchronous,tornado
I am new to Tornado framework, and according to the link Asynchronous and non-Blocking I/O, I wrote some demo code as below. Unfortunately, the sync http client works, but async http client not. It looks like, the callback function that I passed to AsyncHTTPClient.fetch never has the chance to run....

Tornado get_secure_cookie() returning value intermittently

python-2.7,session-cookies,tornado
Having trouble with redirect using get_secure_cookie() in tornado. It seems the get_current_user() method is being called multiple times and intermittently finding data available via self.get_secure_cookie("userid"). The following code let's me visit protected pages after logging in, but won't properly redirect: class BaseHandler(tornado.web.RequestHandler): def get_current_user(self): username, self._user_perms = database.get_user_info(int(self.get_secure_cookie("userid") or 0))...

How to rewrite the url in tornado

nginx,tornado
I'm learning tornado and nginx these days,and I'm confused about the url rewrite. for example:if I wanna make url like this www.abc.com/username/projectname ( which username and projectname are depend on the user ) I got two solution,first one is use Regular Expressions like (r'/(w+)/(w+)', IndexHandler) and trying to get username...

Python: Handle pyMongo's ObjectId with Tornado's get_argument

jquery,python,mongodb,tornado,pymongo
I'm building a REST api with python, tornadoweb and MongoDB. I'm looking for a simple way to handle client-server document exchanges with the lowest operations and the simplest API as possible. I'm facing a problem to serialize and deserialize MongoDB's ObjectId to JSON to send/receive data to and from the...

how to yield gen.coroutine methods

python,tornado
I'm running a Tornado server with the following class: class SessionHandler(tornado.websocket.WebSocketHandler): def open(self): pass def on_close(self): pass @tornado.gen.coroutine def on_message(self,message): #db added to the tornado application object on init self.application.db.add("test") x=self.application.db.get("test") print(x) The get function are follows (part of the db class): @tornado.gen.coroutine def get(self,key): result=[] lookup=yield self.postCol.find_one({"_id":key}) if not...

Tornado and concurrent.futures.Executor

asynchronous,tornado,future
I'm learning about async and Torando and struggling. First off is it possible to use executor class in Tornado? The below example i'm creating a websocket and when receiving a message I want to run check() as another process in the background. This is a contrived example just for my...

my Tornado chat is losing messages

chat,real-time,tornado
i am losing messages in my tornado chat and i do not known how to detect when the message wasn't sent and to send the message again there is any way to detect when the conexion get lost? and when the conexión restart send the message this is my code...

Tornado coroutines don’t work in Cython

python,generator,tornado,cython,coroutine
This code works in Python 3.4.3 using Tornado 4.1 — it sleeps for 1 second and then prints "Hello World! 123". But when compiled using Cython (I tried versions 0.20.1post0 and 0.23dev), it does nothing. import tornado.ioloop import datetime from tornado import gen @gen.coroutine def test(): yield gen.Task(ioloop.add_timeout, datetime.timedelta(seconds=1)) return...

Can a Tornado RequestHandler attend requests, while waiting for a Future to finish?

tornado,nonblocking,coroutine,requesthandler,concurrent.futures
Can a single Tornado RequestHandler class attend new requests, while waiting for a Future to finish in one of its instances? I was debugging a Tornado coroutine that called a ThreadPoolExecutor and I noticed that, while the coroutine was waiting for the executor to finish, the RequestHandler was blocked. So...

Fonts not being served correctly using python-livereload

fonts,tornado,python-sphinx,livereload
I am using Sphinx to build a documentation project. I have created this script to watch the directory and trigger a Sphinx build when changes occur: import os import sys from livereload import Server, shell wd = os.path.dirname(sys.argv[0]) server = Server() server.watch(wd, shell('make.bat clean && make.bat html', cwd=wd), delay=1) server.serve(root='../_build/html',...

Ajax response in tornado some questions about the usage of the self.write

jquery,ajax,tornado
I am still not familiar with the tornado and I am very sorry if there is an obvious answer about the question^_^. I want to get a response from the tornado to the js using the self.write.But when I use the self.write,of course the response is passed to the js...

Flask as proxy doesn't work when deployed to tornado

python,proxy,flask,tornado
I wrote a flask app as kind of a proxy, to analyse the data passing through it and provide a web page where I get the result. All seems to went well when using the default development server that comes with flask, say using: app.run() But when I tried to...

Django asynchronous requests to remote api

python,django,api,asynchronous,tornado
I have the following code (simplified version) for data in my_data_array: res = api_request(data) #write result to db These request may take some time and there are a lot of them. How can I make each iteration of loop asynchronous and send progress with percentage of completed requests to the...

Tornado: pattern for Flask's flashed messages

python,flask,tornado
In Flask, flashing messages on redirect is done using the flash function in the view + {% for message in get_flashed_messages() %} in the template. Tornado doesn't seem to have anything like that built in (which is fine with me, fwiw). Only replacement I've seen so far looks like this...

Tornado: catching exceptions inside asynchronous open

asynchronous,websocket,tornado,coroutine
I have problem with method open of WebSocketHandler. I wrapped it with gen.coroutine for using async call inside (get access to redis). But got another problem, any errors inside open does not catches. Example: @gen.coroutine def open(self): t = 8/0 self._connection_id = yield self._generate_connection_id() self.write_message('...') Method open is call inside...

Hot to use Tornado to get the url parameter?

python,url,tornado
Here is an example www.example.com/abc/def I just wanna get "abc" and "def" without "/".Is it possible? Is it nice if I used self.request.uri and got "abc/def" first ,Then turn it to an string ,and using split('/') to split it so I can get "abc" and "def" I don't feel comfortable...

Can ThreadPoolExecutor help single-threaded application efficiency?

python,multithreading,tornado,event-driven,concurrent.futures
We want to make an e-commerce application, and the team are python devs, but not using python web frameworks (Django/Flask...), and because we found that Tornado was excellent by its simplicity, we gave him a big percentage. But the problem is that, Tornado is single-threaded, and the application will use...

self.get_secure_cookie returns None, but cookie exists

python,cookies,tornado
I have a strange error, when check cookie in Tornado`s WebSocketHandler: class MyHandler(WebSocketHandler): def open(self): print(self.get_secure_cookie("cookie")) It returns None, but in browser I can see that cookie (in another login-handler I use self.set_secure_cookie("cookie", "123")) How to fix that? Thanks! Update if I use self.request.headers I only have: { 'Pragma': 'no-cache',...

How to deploy tornado on openshift

openshift,tornado
When I start tornado application through ssh I get this error: Traceback (most recent call last): File "/var/lib/openshift/54f9750d4382eca672000091/app-root/runtime/repo//app/ws_server.py", line 111, in <module> app.listen(8000) File "/var/lib/openshift/54f9750d4382eca672000091/python/virtenv/venv/lib64/python3.3/site-packages/tornado/web.py", line 1691, in listen server.listen(port, address) File "/var/lib/openshift/54f9750d4382eca672000091/python/virtenv/venv/lib64/python3.3/site-packages/tornado/tcpserver.py", line 125,...

How to deploy tornado page on nginx?

python,nginx,tornado
I am learning the tornado and try to deploy the page on nginx. I open the nginx.conf and then add the the following.. server{ listen 80; server_name _; location / { root /home/ubuntu/tornado; index index.html; } } then I could see the page .... However, the tornado/python code on the...

Simple example of restful web service Python and client basic authentication

python,web-services,tornado,restful-authentication
For study purposes I'd like to know if there is a simple dummy example of how to handle a http request with basic authentication using python. I'd like to follow the same pattern from a example I've found and adapted, as follows: '''webservice.py''' import tornado.httpserver import tornado.ioloop import tornado.web import...

Tornado behind Nginx, should I use TCP nodelay config in python or nginx.conf?

nginx,tcp,websocket,tornado
I have a Tornado server running behind Nginx reverse proxy. The proxy is configured with tcp_nodelay: on. I see there's also an option to call WebSocketHandler.set_nodelay(True) on a WebSocket instance once it's connected. Do these two create the same effect? Are there pro's con's for using one over the other?...

Why tornado doesn't go concurrently?

python,asynchronous,tornado
The following is my test code. I am using Python2.7, with futures installed using: pip install futures The following is my demo code: import tornado.ioloop import tornado.web from tornado.gen import coroutine, Task from tornado.concurrent import Future import time class MainHandler(tornado.web.RequestHandler): @coroutine def get(self): print "in" res = yield Task(self._work) self.write(res)...

How to create a virtual host for a Tornado HTTP server

python,tornado,virtualhost
I want to redirect a random local domain i.e. http://mypage.local to http://localhost/:8888 where I am running a tornado HTTP server that delivers the website. I got all the information from the official docs here. Code see below (main.py). I also added the following line to my /etc/vhosts file: 127.0.0.1:8888 mypage.local...

Post request in angular/tornado website

angularjs,tornado
I have problem with making post request. Below codes fails when user tries to submit the form, tornado handler.get_argument('login') and handler.get_argument('password') both return None, while I clearly send those data. What is even more interesting, when I change method from post to get, everything works fine. How to make it...

Tornado asynchronous job in tornado gen coroutine

python,asynchronous,tornado
I've written an application which takes a job from a queue and executes it asynchronously. def job(self): print 'In job' time.sleep(0.01) @gen.coroutine def start_jobs(self): jobs = filter(lambda x: x['status'] == 0, self.queue) for job in jobs: yield self.job() print 'exit from start job' But, this code does not work. Output:...