Saturday, May 18, 2013

Google App Engine + Python 2.7: Warning: Stripped prohibited headers from URLFetch request: ['Host']

Problem:
Logs show a warning when executing the following code:
import urllib2

url_request = urllib2.Request( url, headers={ "User-Agent" : user_agent } )
url_response = urllib2.urlopen( url_request )
WARNING urlfetch_stub.py:480] Stripped prohibited headers from URLFetch request: ['Host']


Solution:
Use urlfetch instead of urllib2
import urllib
from google.appengine.api import urlfetch

url_response = urlfetch.fetch( url, headers={ "User-Agent" : user_agent } ).content
...
data = urllib.urlencode( { "user": ...} )
url_response = urlfetch.fetch( url, data, urlfetch.POST ).content


Ref:
pythonでgoogleにログインする

No comments: