1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
def post_multipart(host, uri, fields, files):
content_type, body = encode_multipart_formdata(fields, files)
h = http.client.HTTPConnection(host)
headers = {
'User-Agent': 'INSERT USERAGENTNAME',
'Content-Type': content_type
}
h.request('POST', uri, body, headers)
res = h.getresponse()
print(str(body))
return res.status, res.reason, res.read()
def encode_multipart_formdata(fields, files):
BOUNDARY = '----------Boundary'
CRLF = '\r\n'
L = []
lol = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('--' + CRLF)
L.append('Content-Disposition: form-data; name="'+key+'"'+CRLF)
L.append('')
L.append(value)
L.append(CRLF)
for (key, filename, value) in files:
L.append('--' + BOUNDARY+CRLF)
L.append('Content-Disposition: form-data; name="'+filename+'"; filename="'+key+'"'+CRLF)
L.append('Content-Type: "'+get_content_type(filename)+'"'+CRLF)
L.append('')
L.append(value)
L.append(CRLF)
L.append('--' + BOUNDARY + '--'+CRLF)
L.append('')
#body = CRLF.join(str(L)) je crois que cette ligne de marchai pas, c est pour ca que j ai rajouté CRLF a chaques lignes L.append je sais pas si c est bon...
body = str(L)
content_type = 'multipart/form-data; boundary='+BOUNDARY
return content_type, body
body = lol
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
fields = [["picture-b", "1"], ["idpicture]", "0"], ["MAX_FILE_SIZE", "5604304"], ["nb", 556]]
file = open("bastien.png", "rb")
files = [["upload_data", "bastien.png", file]]
post_multipart("post.test.com:80", "/dossier/upload.php", fields, files) |
Partager