Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for A Simple Test HTTP server from Flask
Byron Salty
Byron Salty

Posted on

     

A Simple Test HTTP server from Flask

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)
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

25+ year veteran from the Media industry. Interested in learning, coding and building cool stuff.
  • Location
    Chicago, IL
  • Education
    Georgia Tech, University of Illinois
  • Work
    25+ years in tech. Technology Leader. EdTech. Media.
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp