How to send and receive HTTP POST requests in Python

53,237

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.

Share:
53,237

Related videos on Youtube

Idan S
Author by

Idan S

Updated on January 06, 2022

Comments

  • Idan S
    Idan S over 2 years

    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.

  • Idan S
    Idan S almost 8 years
    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.
  • Artemis
    Artemis almost 8 years
    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.
  • Idan S
    Idan S almost 8 years
    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)
  • Artemis
    Artemis almost 8 years
    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']
  • Idan S
    Idan S almost 8 years
    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?
  • Idan S
    Idan S almost 8 years
    And in the server side, the app.route('/'), i need to replace it with the url?
  • Idan S
    Idan S almost 8 years
    Also getting errror when running client side ,"The requested URL '/' was not found"
  • Artemis
    Artemis almost 8 years
    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.
  • Idan S
    Idan S almost 8 years
    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 all
  • Artemis
    Artemis almost 8 years
    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 it
  • Long
    Long almost 5 years
    Please fix this @app.route('/post', methods=['POST']). The route should be '/post' not '/'