Skip to content

Instantly share code, notes, and snippets.

@jul
Created October 24, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jul/e17f05d4af6b1bd6a620ca197d40633c to your computer and use it in GitHub Desktop.
Save jul/e17f05d4af6b1bd6a620ca197d40633c to your computer and use it in GitHub Desktop.
a simple multipart-form-data wsgi server echoing path, file and data sent to it (for testing web-client)
import multipart
from wsgiref.simple_server import make_server
from json import dumps
from urllib.parse import parse_qsl
import logging
def simple_app(environ, start_response):
fo,fi=multipart.parse_form_data(environ)
def retfi(fi):
return {
'name' : fi.filename,
'content' : fi.file.read().decode('utf-8', 'backslashreplace'),
'content_type' : fi.content_type,
}
fo.update(**{ k: retfi(v) for k,v in fi.items()})
fo=dict(fo.items())
#from pdb import set_trace;set_trace()
fo["_path"]=environ["PATH_INFO"]
fo.update(**dict(parse_qsl(environ["QUERY_STRING"])))
fo.update(**dict(mfct=environ.get("CONTENT_TYPE","none")))
res=dumps(fo, indent=4)
start_response('200 OK', [('Content-type', 'text/plain; charset=utf-8')])
return [dumps(fo, indent=4).encode('utf-8', 'backslashreplace')]
httpd = make_server('', 5000, simple_app)
print("Serving on port 5000...")
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment