If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Post / get request

Started by nicolebeckett, Feb 02, 2023, 04:41 AM

Previous topic - Next topic

nicolebeckettTopic starter

I've started my journey to learn Python and everything was going well until I faced a problem - how can I accept requests in a script? In PHP, it's done by using $_POST['lalala'] for POST requests and $_GET['lalala'] for GET requests. However, I'm not sure how to do it in Python. I've been told that the requests module is needed and have downloaded and installed it on my hosting. I've managed to learn how to send requests using the module, but I'm still unable to find any information on how to receive requests.

I've discovered that using the line 'attat = sys.stdin.read()', I can accept the query string, which will be equal to attat. However, it's not very convenient. Is there a Python solution similar to the one in PHP for accepting POST requests? As for GET requests, I'm not sure what to do at all.
  •  

feedar

HTTP clients send HTTP requests and there is a library called 'requests' that can help in creating an HTTP client.

When an HTTP request, such as GET/POST, is made, it is answered by an HTTP server (such as Apache, Nginx, or Gunicorn) which can run Python code using CGI or WSGI interfaces. In Python, there are several web frameworks such as Bottle, Flask, Pyramid, and Django that provide a higher-level interface for developing web applications. Additionally, there are also more general network libraries like Twisted, Tornado, and Gevent.
  •  

Brijesh

It might be worth looking into some web frameworks such as Bottle, Flask, or Django.

When using the GET or POST form method, you can print the username using the following code:
print request.GET['username'] # for GET form method
print request.POST['username'] # for POST form method
  •  

UWZLaltawataSopy

To handle POST requests in Python, you can use the Flask framework, which is a popular web framework for Python. First, you need to install Flask using pip. Then you can create a route in your Python script to handle POST requests. Here's an example of how you can do this:

from flask import Flask, request

app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit_form():
    data = request.form['lalala']
    # Process the data from the POST request
    return 'Data received: ' + data

if __name__ == '__main__':
    app.run()


In the above example, when a POST request is made to the /submit endpoint, the data from the 'lalala' field in the form is accessed using `request.form['lalala']`. You can then process this data as needed.

Now, for handling GET requests in Python, you can use the same Flask framework. Here's an example of how you can create a route to handle GET requests:

from flask import Flask, request

app = Flask(__name__)

@app.route('/search', methods=['GET'])
def search():
    query = request.args.get('q')
    # Process the query from the GET request
    return 'Search query: ' + query

if __name__ == '__main__':
    app.run()


In the above example, when a GET request is made to the /search endpoint with a query parameter, the query is accessed using `request.args.get('q')`. You can then process this query in your Python script.

Using the Flask framework makes it easy to handle both POST and GET requests in Python, providing a clean and convenient solution similar to handling requests in PHP.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...