Skip to content

Instantly share code, notes, and snippets.

@atomd-zz
Created February 3, 2013 12:01
Show Gist options
  • Save atomd-zz/4701513 to your computer and use it in GitHub Desktop.
Save atomd-zz/4701513 to your computer and use it in GitHub Desktop.
wsgi file upload
# -*- coding: utf-8 -*-
import httplib
import sys
import mimetypes
import os
CHUNKSIZE = 65563
url="http://127.0.0.1:8090/"
conn = httplib.HTTPConnection("127.0.0.1", 8090)
conn.putrequest("PUT", "/")
conn.putheader("Content-Type", "application/zip")
conn.putheader("Transfer-Encoding", "chunked")
conn.endheaders()
fp = '/home/atomd/Desktop/ubuntu-12.04.1-desktop-i386.iso'
f = os.path.basename(fp)
print f
print mimetypes.guess_type(f)
with open(f, 'rb') as fp:
chunk = fp.read(CHUNKSIZE)
while chunk:
conn.send('%x\r\n%s\r\n' % (len(chunk), chunk))
chunk = fp.read(CHUNKSIZE)
conn.send('0\r\n\r\n')
response = conn.getresponse()
print response.read()
# -*- coding: utf-8 -*-
import eventlet
from eventlet import wsgi
from webob import Response
def save_file(environ, start_response):
CHUNKSIZE = 65563
f = open("temp_file", "wb")
chunk = environ["wsgi.input"].read(CHUNKSIZE)
while chunk:
f.write(chunk)
chunk = environ["wsgi.input"].read(CHUNKSIZE)
f.close()
response = Response("success")
return response(environ, start_response)
if __name__ == "__main__":
wsgi.server(eventlet.listen(('127.0.0.1', 8090)), save_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment