
Last night I found myself needing to update some app code to switch from a synchronous http call to an async one. This is not a particularly hard problem but it can be tricky to get correct.
What I needed therefore was a way to test these http calls, and the machine I was using didn't already have any http servers running to use as the target.
No problem: it has Python - and that's all you need.
Don't Install - Just create
This pattern has been forming for me. For simple tasks, it's often easier to just write a little utility script instead of installing some full blown software to do a job.
If you need a real server, by all means go for it (but even then I'd suggest Docker if possible). But if you just need a quick endpoint to test with or to solve a single use case then Python is probably your friend.
I'm saying Python specifically because it's rather ubiquitous with a rich standard library and ecosystem.
Server code
The idea with this simple server was to help me test the async client calls so I wanted it to wait 5 seconds and then respond. I just wanted it to respond to any standard call with a 200 after 5 seconds.
fromflaskimportFlask,requestimporttimeapp=Flask(__name__)@app.route('/',defaults={'path':''},methods=['GET','POST','PUT','DELETE','PATCH'])@app.route('/<path:path>',methods=['GET','POST','PUT','DELETE','PATCH'])defcatch_all(path):print(f"Path:{path}")print(f"Headers:{dict(request.headers)}")print(f"Params:{request.args}")print(f"Data:{request.data}")# Wait for 5 secondstime.sleep(5)return"yessir"if__name__=='__main__':app.run(host='0.0.0.0',port=5000)
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse