How to send and receive HTTP POST requests in Python [closed]
Asked Answered
O

1

27

I need a simple Client-side method that can send a boolean value in a HTTP POST request, and a Server-side function that listens out for, and can save the POST content as a var.

I am having trouble finding information on how to use the httplib.

Please show me a simple example, using localhost for the http connection.

Oversight answered 28/6, 2016 at 7:59 Comment(0)
R
34

For the client side, as a built-in option you'd use urllib.request module. For an even higher-level client, try requests. It is quite intuitive and easy to use/install.

For the server side, I'll recommend you to use a small web framework like Flask, Bottle or Tornado. These ones are quite easy to use, and lightweight.

For example, a small client-side code to send the post variable foo using requests would look like this:

import requests
r = requests.post("http://yoururl/post", data={'foo': 'bar'})
# And done.
print(r.text) # displays the result body.

And a server-side code to receive and use the POST request using flask would look like this:

from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def result():
    print(request.form['foo']) # should display 'bar'
    return 'Received !' # response to your request.

This is the simplest & quickest way to send/receive a POST request using python.

Relegate answered 28/6, 2016 at 8:15 Comment(11)
Hey! Thanks for the ans, but i think i wasnt clear enough about what i need to do. I having for example, one computer running the servers script, and another computer runnig client script.The client need to send a true var for if he is alive and this need to be an http request, so i dont really have a url, just an ip address.Oversight
Url can of course be an ip address. The only thing you need to change is instead of putting yoururl you'll do yourip. Domain names are just a literal representation of an ip, therefore working the same way when using networks.Relegate
Sorry for asking this stupid questions, but im a little bit confused. What is the r.text, in the clients side, the foo and the bar, what they represent? (And dont know if the changes something but both scripts running on linux, the server side is running on a linux with appache installed)Oversight
No problem. When sending requests using the requests library, you have a result as an object (stored in the variable "r" on the example). One of the properties of this object is "text", containing the result body (html as response for example). Foo and bar are just a random example of a key/value pair (imagine if you do a form, with user and password field, request.form['user'] will give you the data entered in the field, same for request.form['password']Relegate
Thanks alot! And one more question, When i install apache on my linux server and now he is a web server, he knows how to recive post/get requests, or you need to write something in PHP or some thing like this?Oversight
And in the server side, the app.route('/'), i need to replace it with the url?Oversight
Also getting errror when running client side ,"The requested URL '/' was not found"Oversight
If you are using flask as the web server, I don't understand why you would use apache for it. These are 2 completely different servers. Flask takes the place of apache/php, and in app.route you need to put the uri path.Relegate
Oh now it makes sense, its critical to me i use the appache server, so how can i do this? The thing is this : I have client that need to talk with the server whos running apache on him, he need to tell him "im alive" using http requests, thats allOversight
Well, you can create an apache rule to use apache as the wsgi server for flask, but you'll have to search by yourself, don't know how to do itRelegate
Please fix this @app.route('/post', methods=['POST']). The route should be '/post' not '/'Southward

© 2022 - 2024 — McMap. All rights reserved.