Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use POST method in Tornado?

Tags:

I'm trying to use Tornado to start a server and post a string to it. I've found lots of examples of how to write the post method in the handler class, but no examples of how to write the post request. My current code does cause the post method to execute, but get_argument isn't getting the data--it just prints the default "No data received" every time. What am I doing wrong?

My code looks like this:

class MainHandler(tornado.web.RequestHandler):     def post(self):         data = self.get_argument('body', 'No data received')         self.write(data)  application = tornado.web.Application([     (r"/", MainHandler), ])  if __name__ == "__main__":      def handle_request(response):         if response.error:             print "Error:", response.error         else:             print response.body         tornado.ioloop.IOLoop.instance().stop()      application.listen(8888)         test = "test data"     http_client = tornado.httpclient.AsyncHTTPClient()     http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, body=test)     tornado.ioloop.IOLoop.instance().start() 

Is putting the string I want to send in the "body" parameter the right thing to do? In some examples I've seen, like here, it seems people create their own parameters, but if I try to add a new parameter to the request, like

http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, data=test) 

I just get an error saying "TypeError: init() got an unexpected keyword argument 'data'"

Thanks!

like image 588
user1363445 Avatar asked Apr 28 '12 22:04

user1363445


People also ask

What is Tornado web application?

A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server.

How do I run a tornado web server?

If you want to daemonize tornado - use supervisord. If you want to access tornado on address like http://mylocal.dev/ - you should look at nginx and use it like reverse proxy. And on specific port it can be binded like in Lafada's answer.

What is Tornado Django?

Tornado is a web server and a web framework by most definitions, but it's quite a minimal framework (compared to Rails or Django). Tornado modules are loosely coupled, so it's possible to use just the web server component (or even just the lower level IO loop).


1 Answers

it seems people create their own parameters

Not quite. From the docs:

fetch(request, **kwargs)

Executes a request, returning an HTTPResponse.

The request may be either a string URL or an HTTPRequest object. If it is a string, we construct an HTTPRequest using any additional kwargs: HTTPRequest(request, **kwargs)

(Link)

So the kwargs are actually from this method.

Anyways, to the real meat of the problem: How do you send POST data? You were on the right track, but you need to url encode your POST data and use that as your body kwarg. Like this:

import urllib post_data = { 'data': 'test data' } #A dictionary of your post data body = urllib.urlencode(post_data) #Make it into a post request http_client.fetch("http://0.0.0.0:8888", handle_request, method='POST', headers=None, body=body) #Send it off! 

Then to get the data:

data = self.get_argument('data', 'No data received') 
like image 150
Theron Luhn Avatar answered Oct 13 '22 07:10

Theron Luhn