How to send and receive HTTP POST requests in Python

I need a simple client method that can send a boolean in an HTTP POST request and a server-side function that listens and can save the contents of the POST as var.

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

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

+4
source share
1 answer

For the client side, you can execute all kinds of requests using this python: requests library . It is quite intuitive and easy to use / install.

-, Flask, Bottle Tornado. .

, post foo :

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

POST :

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.

/ POST python.

+6

All Articles