Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created October 28, 2014 01:53
Show Gist options
  • Save taojy123/4ed4c9c61b2af1465f18 to your computer and use it in GitHub Desktop.
Save taojy123/4ed4c9c61b2af1465f18 to your computer and use it in GitHub Desktop.
Python 模拟POST发包上传文件
# requests
import requests
r = requests.post("http://127.0.0.1:8000/input_file/", files={"file": open("a.txt", "rb")})
print r.url
print r.text
# poster
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
register_openers()
datagen, headers = multipart_encode({"file": open("a.txt", "rb")})
request = urllib2.Request("http://127.0.0.1:8000/input_file/", datagen, headers)
s = urllib2.urlopen(request).read()
print s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment