Frage

Ich versuche, eine mehrteilige Form zu schreiben httplib verwendet, wird URL auf Google App Engine gehostet wird, auf Post es sagt Methode nicht erlaubt, obwohl die Post Arbeiten mit urllib2. Vollarbeitsbeispiel angebracht ist.

Meine Frage ist, was ist der Unterschied zwischen beiden, warum man arbeitet aber nicht die andere

  1. gibt es ein Problem in meiner mulipart Form Postleitzahl?

  2. oder das Problem mit Google App Engine?

  3. oder etwas anderes?


import httplib
import urllib2, urllib

# multipart form post using httplib fails, saying
# 405, 'Method Not Allowed'
url = "http://mockpublish.appspot.com/publish/api/revision_screen_create"
_, host, selector, _, _ = urllib2.urlparse.urlsplit(url)
print host, selector
h = httplib.HTTP(host)

h.putrequest('POST', selector)

BOUNDARY = '----------THE_FORM_BOUNDARY'
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
h.putheader('content-type', content_type)
h.putheader('User-Agent', 'Python-urllib/2.5,gzip(gfe)')
content = ""
L = []
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="test"')
L.append('')
L.append("xxx")
L.append('--' + BOUNDARY + '--')
L.append('')
content = '\r\n'.join(L)
h.putheader('content-length', str(len(content)))
h.endheaders()
h.send(content)

print h.getreply()

# post using urllib2 works
data = urllib.urlencode({'test':'xxx'})
request = urllib2.Request(url)
f = urllib2.urlopen(request, data)
output = f.read()
print output

Edit: Nach dem Wechsel putrequest auf Anfrage (auf Nick Johnson Vorschlag), es funktioniert

url = "http://mockpublish.appspot.com/publish/api/revision_screen_create"
_, host, selector, _, _ = urllib2.urlparse.urlsplit(url)

h = httplib.HTTPConnection(host)

BOUNDARY = '----------THE_FORM_BOUNDARY'
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY

content = ""
L = []
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="test"')
L.append('')
L.append("xxx")
L.append('--' + BOUNDARY + '--')
L.append('')
content = '\r\n'.join(L)
h.request('POST', selector, content,{'content-type':content_type})
res = h.getresponse()
print res.status, res.reason, res.read()

so nun bleibt die Frage, was ist der Unterschied zwischen beiden Ansätzen und können zunächst erste arbeiten werden?

War es hilfreich?

Lösung

Nick Johnson Antwort

Haben Sie versucht, die Anfrage mit httplib Senden mit .request () anstelle von .putrequest () usw., die Header als dict Versorgung?

es funktioniert!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top