cphill
cphill

Reputation: 5914

Python Local Server For Callback URL

I am working on an oauth project that requires me to have a callback url that can be loaded upon successful authorization in order to request access tokens, but I'm not sure how I can run that server in the proper manner. I am familiar with the useful one-line python server setup python -m http.server, but this will just load the directory in which I started the server and act as a server for navigating the files within that directory.

Is there a preferred way to set up a simple server that can be used for this redirect process and make the additional server call I need? Would I need to use a web framework like Django?

Upvotes: 2

Views: 4321

Answers (1)

furas
furas

Reputation: 142631

Using python -m http.server you can serve only static files but you need to run some code which gets argument(s) and uses it.

You could use option --cgi in python -m http.server --cgi and then you could put script (in any language) in folder cgi-bin and run it http://localhost/cgi-bin/script.py.

But method with CGI is very old and it can be much easier to use some of web microframework like Flask or bottle

script.py

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    print('args:', request.args)  # display text in console
    #print('form:', request.form)
    #print('data:', request.data)
    #print('json:', request.json)
    #print('files:', request.files)
    
    return request.args.get('data', 'none')  # send text to web browser

if __name__ == '__main__':
    app.run(port=80, debug=True)

And run it as python script.py and test in web browser

http://127.0.0.1/?data=qwerty

And request.args.get("data") should gives you qwerty which you can use in Python code.

Upvotes: 1

Related Questions