NORXND
NORXND

Reputation: 103

Web server in python for responding to GET and POST

I want to create web server, and listen now.

  1. Server must have different functions for each endpoint (and method).

  2. I want to get (e.g. to variable) parameters (and data if POST)

  3. Respond to get (and POST if its possible)

  4. Respond in JSON

Someone can help me with this?

PS: I will be run it on Heroku, and send requests to it via Roblox's HttpService

Upvotes: 1

Views: 798

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169364

Below see examples of each of your requirements using the Flask lightweight web framework.

After that is a link to a short description of how to deploy to Heroku.

# app.py

from flask import Flask 
from flask import request, render_template

app = Flask(__name__)

@app.route('/test-get-request-parameters')
def test_get_request_parameters():
    # 1. different function per endpoint 
    # 2. GET parameter to variable 
    # 3. respond to GET
    var = request.args.get('some_request_variable')
    return render_template('hello_world.html')

@app.route('/test-post-method',methods=['POST'])
def test_post_method():
    # 2. receive POST data 
    # 3. respond to POST
    print(request.get_json())
    return 'hello, world!'

@app.route('/test-get-or-post', methods=['GET','POST'])
def test_get_or_post():
    # 4. respond in JSON
    if request.method == 'POST':
        d = {'hello':'world'}
        return d # this will be JSON response
    return render_template('test.html')

To deploy to Heroku you need a Procfile with something like this in it:

web: gunicorn app:app

And you can follow these instructions: https://devcenter.heroku.com/articles/getting-started-with-python

Upvotes: 1

Related Questions