Created
February 3, 2013 12:01
-
-
Save atomd-zz/4701513 to your computer and use it in GitHub Desktop.
wsgi file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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