Jeren Jeren - 9 months ago 59
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):
query = "select * from geo.places where text ='"+word+"'"''+query+'&diagnostics=true').read()
print sourceCode
except Exception, e:
print str(e)

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

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

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

Answer Source

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 = ''
>>> url = baseurl + query
>>> url
" * 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="" yahoo:count="10" yahoo:created="2014-05-17T21:12:52Z" yahoo:lang="en-US"><results><place xmlns="" xml:lang="en-US" yahoo:uri=""><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.