2

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?

cphill
  • 5,596
  • 16
  • 89
  • 182
  • you need server which can run code on server (Python/PHP/Ruby/etc.) You could use option `--cgi` (`python -m http.server --cgi`), and then it can run scripts in folder `cgi-bin` - `http://localhost/cgi-bin/script.py`. But `CGI` method is very old method and probably more useful can be to use something newer like web framework `Flask`, `Bottle`, `Django` – furas Sep 15 '20 at 14:06

1 Answers1

1

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.

furas
  • 134,197
  • 12
  • 106
  • 148