문제

httplib을 사용하여 멀티-파트 양식을 게시하려고합니다. URL은 Google App Engine에서 호스팅됩니다. 게시물에서는 URLLIB2를 사용하는 게시물이 작동하지만 메소드가 허용되지 않습니다. 전체 작업 예제가 첨부되어 있습니다.

제 질문

  1. Mulipart 양식 우체국에 문제가 있습니까?

  2. 아니면 Google App Engine에 문제가 있습니까?

  3. 또는 다른 것 ?


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

편집 : Putrequest가 요청하도록 변경 한 후 (Nick Johnson의 제안에 따라) 작동합니다.

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()

이제 질문은 두 가지 접근 방식의 차이점이 무엇이며 먼저 작업을 먼저 할 수 있습니까?

도움이 되었습니까?

해결책

Nick Johnson 's 대답

.putRequest () 등 대신 .request ()를 사용하여 httplib로 요청을 보내려고 했습니까?

효과가있다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top