Jeren Jeren - 1 month ago 5
Python Question

Python HTTP Error 505: HTTP Version Not Supported

I have the following code:

import re
from re import sub

import cookielib
from cookielib import CookieJar
import urllib2
from urllib2 import urlopen

cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('user-agent' , 'Safari/7.0.2')]

def check(word):
try:
query = "select * from geo.places where text ='"+word+"'"
sourceCode=opener.open('http://query.yahooapis.com/v1/public/yql?q='+query+'&diagnostics=true').read()
print sourceCode
except Exception, e:
print str(e)
print 'ERROR IN MAIN TRY'

myStr = ['I','went','to','Boston']
for item in myStr:
check(item)


I am trying to query
select * from geo.places where text = 'Boston'
(for example).

I keep receiving this error:

HTTP Error 505: HTTP Version Not Supported
ERROR IN MAIN TRY


What can cause this error and how can I solve it?

Answer

Not sure, what is going wrong, but when I try to do the same action using requests library, it works:

>>> import requests
>>> word = "Boston"
>>> query = "select * from geo.places where text ='"+word+"'"
>>> query
"select * from geo.places where text ='Boston'"
>>> baseurl = 'http://query.yahooapis.com/v1/public/yql?q='
>>> url = baseurl + query
>>> url
"http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text ='Boston'"
>>> req = requests.get(url)
>>> req
<Response [200]>
>>> req.text
u'<?xml version="1.0" encoding="UTF-8"?>\n<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="10" yahoo:created="2014-05-17T21:12:52Z" yahoo:lang="en-US"><results><place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/2367105"><woeid>2367105</woeid><placeTypeName code="7">Town</placeTypeName><name>Boston</name><country code="US" type="Country" woeid="23424977">United States</country><admin1 code="US-MA" type="State" woeid="2347580">Massachusetts</admin1><admin2 code="" type="County" woei....

Note, that there are differences, my code is much simpler, it does not work with cookies and it does not try to pretend Safari browser.

If you need to use cookies with requests, you will find very good support for it there.

Comments