Ciao, ho alcuni problemi per effettuare l'upload da un programma in python a una pagina php tramite il protocollo http, in python vorrei fare la richiesta mentre in php vorrei fare l'upload vero e proprio:
(Ho trovato poco in giro)
Python
codice:
import httplib, mimetypes,os
def post_multipart(host, selector):
content_type, body = encode_multipart_formdata()
h = httplib.HTTPConnection(host)
headers = {
'User-Agent': 'Prova upload',
'Content-Type': content_type
}
h.request('POST', selector, body, headers)
res = h.getresponse()
return res.status, res.reason, res.read()
def encode_multipart_formdata():
BOUNDARY = '1484868717354'
CRLF = '\r\n'
L = []
fields = ""
key = "name"
value = ""
filename= "logo2.png"
handle = open (filename,'r')
value += handle.read()
handle.close()
filesize = os.stat(filename).st_size
L.append('Content-Disposition: form-data; boundary="-----------------------------"' + BOUNDARY)
L.append('Content-Length: %s' % (filesize))
L.append('')
L.append('-----------------------------' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(value)
L.append('-----------------------------' + BOUNDARY + '--')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
print post_multipart("host","upload1.php")
La pagina php funziona se faccio l'upload semplice con il classico form html, mentre provando così, ottengo:
codice:
(400, 'Bad Request', '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n
Your browser sent a request that this server could not understand.
\n</p>\n
Additionally, a 403 Forbidden\nerror was encountered while trying to use an ErrorDocument to handle the request.</p>\n</body></html>\n')
Per conoscere il modo in cui opera il protocollo http, ho fatto una richiesta con il browser a una specie di server in php che ho programmato, ottenendo:
-Firefox
codice:
POST / HTTP/1.1
Host: localhost:8082
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:85/upload.php
Content-Type: multipart/form-data; boundary=---------------------------1484868717354
Content-Length: 23350
-----------------------------1484868717354
Content-Disposition: form-data; name="name"; filename="640321.jpg"
Content-Type: image/jpeg
i caratteri che formano il file
-----------------------------1484868717354
Content-Disposition: form-data; name="upload"
Carica file
-----------------------------1484868717354--
name="upload" è il name del tasto submit
mentre name="name" dovrebbe essere $_FILES['name']
-con il mio in python
codice:
POST upload1.php HTTP/1.1
Host: localhost:8082
Accept-Encoding: identity
Content-Length: 59970
Content-Type: multipart/form-data; boundary=1484868717354
User-Agent: Prova upload
Content-Disposition: form-data; boundary="-----------------------------"1484868717354
Content-Length: "59672"
-----------------------------1484868717354
Content-Disposition: form-data; name="name"; filename="logo2.png"
Content-Type: image/png
i caratteri che formano il file.....
-----------------------------1484868717354--
Spero qualcuno possa aiutarmi, non so dove sbaglio nella richiesta.
Penso che la sezione sia adatta.
Saluti
Alberto