urllib2.urlopen('ur') gives error - python

I am new to python and trying to extract the contents of a page. When I do urlopen('http://www.google.com'), I get the following error :
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1185, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1160, in do_open
raise URLError(err)
Any solutions to this ?

that error message occurs if your net is offline

Related

can't download a file with python but i can from the browser

I am trying to make a function that will download file from the internet. If I go to the direct web address I do get the images or download the files. But, when I am running my code, it just hangs and then I get the timeout error. Is there any particular reason why that might be happening?
#fpp = "http://www.blog.pythonlibrary.org/wp-content/uploads/2012/06/wxDbViewer.zip"
fpp = "http://www.gunnerkrigg.com//comics/00000001.jpg"
download_file(fpp)
This is my function:
import urllib2
def download_file(url_path):
response = urllib2.urlopen(url_path)
data = response.read()
Is there any particular reason why might work from the browser but not in the code?
this is the error i get:
Traceback (most recent call last):
File "/Users/dk/testing/myfile.py", line 42, in <module>
download_file(fpp)
File "/Users/dk/Documents/testing/code_project.py", line 154, in download_file
response = urllib2.urlopen(url_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 60] Operation timed out>

urllib2.URLError: <urlopen error unknown url type: https> on openwrt

I'm trying to build python script on OpenWRT and I have this error. I checked if python is compiled with ssl support and it's okay. I still don't find what's wrong, any help?
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.21.tar.gz
Traceback (most recent call last):
File "./setup.py", line 2, in <module>
use_setuptools()
File "/home/ubuntu/Bureau/openwrt/build_dir/target-mipsel_r2_uClibc-0.9.32/pyowncloud/distribute_setup.py", line 152, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "/home/ubuntu/Bureau/openwrt/build_dir/target-mipsel_r2_uClibc-0.9.32/pyowncloud/distribute_setup.py", line 131, in _do_download
to_dir, download_delay)
File "/home/ubuntu/Bureau/openwrt/build_dir/target-mipsel_r2_uClibc-0.9.32/pyowncloud/distribute_setup.py", line 201, in download_setuptools
src = urlopen(url)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 438, in error
result = self._call_chain(*args)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 625, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 423, in _open
'unknown_open', req)
File "/home/ubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/home/dubuntu/Bureau/openwrt/staging_dir/target-mipsel_r2_uClibc-0.9.32/usr/lib/python2.7/urllib2.py", line 1240, in unknown_open
raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: https>

splinter Python Phantomjs crashes sometime

I am getting this error while running this code.I am using splinter phantomjs. sometimes it works correctly but sometimes it shows some error.
Error:
raised unexpected: BadStatusLine("''",)
Traceback (most recent call last):
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/home/synerzip/forestway/app/services/connection.py", line 121, in find_connection_for_company
self.browser.find_by_css("img.entity-img").click()
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/splinter/driver/webdriver/__init__.py", line 539, in click
self._element.click()
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
return self._parent.execute(command, params)
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 231, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/home/synerzip/forestway/flask/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 463, in _request
resp = opener.open(request, timeout=self._timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1214, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1187, in do_open
r = h.getresponse(buffering=True)
File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 415, in begin
version, status, reason = self._read`enter code here`_status()
File "/usr/lib/python2.7/httplib.py", line 379, in _read_status
raise BadStatusLine(line)
BadStatusLine: ''

Error while loading MNIST database in Python

I'm trying to work with MNIST handwritten digits database but the below two lines are giving me error. Where am I going wrong??
from sklearn import datasets
dataset = datasets.fetch_mldata('MNIST original')
The error which I am getting is as followed:
Traceback (most recent call last):
File "E:/HWrecognition/deep-belief-network-gpu-update/kk.py", line 5, in <module>
dataset = datasets.fetch_mldata('MNIST original')
File "C:\Python27\lib\site-packages\sklearn\datasets\mldata.py", line 142, in fetch_mldata
mldata_url = urlopen(urlname)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 397, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 429, in error
result = self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 605, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Python27\lib\urllib2.py", line 391, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 409, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1173, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python27\lib\urllib2.py", line 1148, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10109] getaddrinfo failed>

Python : TLS/SSL connection has been closed

My code
conn = __get_s3_connection(s3_values.get('accessKeyId'), s3_values.get('secretAccessKey'))
key = s3_values.get('proposal_key') + proposal_unique_id + s3_values.get('proposal_append_path')
request = urllib2.Request(conn.generate_url(s3_values.get('expires_in'), 'GET', bucket=s3_values.get('bucket'), key=key))
request.add_header('Accept-encoding', 'gzip')
response = urllib2.urlopen(request)
The url looks like https://production.myorg.s3.amazonaws.com/key/document.xml.gz?Signature=signature%3D&Expires=1349462207&AWSAccessKeyId=accessId
This method was working fine until 1 hour back, but when I run the same program, it throws
Traceback (most recent call last):
File "/Users/hhimanshu/IdeaProjects/analytics/src/utilities/documentReader.py", line 145, in <module>
main()
File "/Users/hhimanshu/IdeaProjects/analytics/src/utilities/documentReader.py", line 141, in main
x = get_proposal_data_from_s3('documentId')
File "/Users/hhimanshu/IdeaProjects/analytics/src/utilities/documentReader.py", line 54, in get_proposal_data_from_s3
response = urllib2.urlopen(request)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 392, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 370, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1194, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1161, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 6] _ssl.c:503: TLS/SSL connection has been closed>
What could be the reason? How can I avoid this situation?
This was because of intermittent internet connection. Resolved on it own

Categories

Resources