urlopen error
#1
My addon.py says:

import urllib2
import xbmcgui
import xbmcaddon

url = 'http://www.newgrounds.com/audio/listen/642415'
req = urllib2.Request(url)
response = urllib2.urlopen(url)

Fails every time. The request works, but the urlopen causes an error. Why?
Reply
#2
After reading the log I found this:

07:19:42 T:11252 NOTICE: -->Python Interpreter Initialized<--
07:19:43 T:11252 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <class 'urllib2.HTTPError'>
Error Contents: HTTP Error 403: Forbidden
Traceback (most recent call last):
File "C:\Users\jake.CUTTING\AppData\Roaming\Kodi\addons\plugin.program.newgrounds\addon.py", line 12, in <module>
response = urllib2.urlopen(url)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Program Files (x86)\Kodi\system\python\Lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
-->End of Python script error report<--

I am getting an error 403. So I read some tutorials that say to change the user agent.

import urllib2,cookielib
import xbmcgui
import xbmcaddon

url = 'http://www.newgrounds.com/audio/listen/642415'

hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}

req = urllib2.Request(url, headers=hdr)

try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()

content = page.read()
xbmcgui.Dialog().ok('test', content)

That got it working. Sorry I didn't realize there was a log file.
Reply

Logout Mark Read Team Forum Stats Members Help
urlopen error0