Forums

Connection Refused Error

I am getting a connection refused error on trying to download a zipfile from a website.Heres the code

from zipfile import ZipFile

from urllib.request import urlopen
import pandas as pd

import os

URL = \ 'http://ratedata.gaincapital.com/2017/01%20January/EUR_JPY_Week1.zip'

open and save the zip file onto computer

url = urlopen(URL)

output = open('zipFile.zip', 'wb') # note the flag: "wb"

output.write(url.read())

output.close()

read the zip file as a pandas dataframe

df = pd.read_csv('zipFile.zip') # pandas version 0.18.1 takes zip files

from zipfile import ZipFile from urllib.request import urlopen
import pandas as pd import os

URL = \ 'http://ratedata.gaincapital.com/2017/01%20January/EUR_JPY_Week1.zip'

open and save the zip file onto computer

url = urlopen(URL) output = open('zipFile.zip', 'wb') # note the flag: "wb"
output.write(url.read()) output.close()

read the zip file as a pandas dataframe

df = pd.read_csv('zipFile.zip') # pandas version 0.18.1 takes zip files

This is the error message

ConnectionRefusedError Traceback (most recent call last) /usr/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 1253 try: -> 1254 h.request(req.get_method(), req.selector, req.data, headers) 1255 except OSError as err: # timeout error

/usr/lib/python3.5/http/client.py in request(self, method, url, body, headers) 1105 """Send a complete request to the server.""" -> 1106 self._send_request(method, url, body, headers) 1107

/usr/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers) 1150 body = _encode(body, 'body') -> 1151 self.endheaders(body) 1152

/usr/lib/python3.5/http/client.py in endheaders(self, message_body) 1101 raise CannotSendHeader() -> 1102 self._send_output(message_body) 1103

/usr/lib/python3.5/http/client.py in _send_output(self, message_body) 933 --> 934 self.send(msg) 935 if message_body is not None:

/usr/lib/python3.5/http/client.py in send(self, data) 876 if self.auto_open: --> 877 self.connect() 878 else:

/usr/lib/python3.5/http/client.py in connect(self) 848 self.sock = self._create_connection( --> 849 (self.host,self.port), self.timeout, self.source_address) 850 self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

/usr/lib/python3.5/socket.py in create_connection(address, timeout, source_address) 710 if err is not None: --> 711 raise err 712 else:

/usr/lib/python3.5/socket.py in create_connection(address, timeout, source_address) 701 sock.bind(source_address) --> 702 sock.connect(sa) 703 return sock

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

URLError Traceback (most recent call last) <ipython-input-2-46fb38c4ce3b> in <module>() 7 8 # open and save the zip file onto computer ----> 9 url = urlopen(URL) 10 output = open('zipFile.zip', 'wb') # note the flag: "wb" 11 output.write(url.read())

/usr/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 161 else: 162 opener = _opener --> 163 return opener.open(url, data, timeout) 164 165 def install_opener(opener):

/usr/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout) 464 req = meth(req) 465 --> 466 response = self._open(req, data) 467 468 # post-process response

/usr/lib/python3.5/urllib/request.py in _open(self, req, data) 482 protocol = req.type 483 result = self._call_chain(self.handle_open, protocol, protocol + --> 484 '_open', req) 485 if result: 486 return result

/usr/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, args) 442 for handler in handlers: 443 func = getattr(handler, meth_name) --> 444 result = func(args) 445 if result is not None: 446 return result

/usr/lib/python3.5/urllib/request.py in http_open(self, req) 1280 1281 def http_open(self, req): -> 1282 return self.do_open(http.client.HTTPConnection, req) 1283 1284 http_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.5/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 1254 h.request(req.get_method(), req.selector, req.data, headers) 1255 except OSError as err: # timeout error -> 1256 raise URLError(err) 1257 r = h.getresponse() 1258 except:

URLError: <urlopen error [Errno 111] Connection refused>

I see that you upgraded to a paid account today (thanks!). Are you running the script in a console that you started before you upgraded? If so, it will be running with the old settings from when you had a free account, which would explain the problem.

If you start a new console and run it from there, the problem should be fixed.

No I am running it on an ipython notebook and i started coding on the platform only after making the payments.

Ah, right. Let me take a look. Sometimes IPython notebooks don't pick up the new settings after you upgrade your account. I'll see if that's the case and post back here.

Yes, it looks like that was the problem here. Sorry about that! I've kicked the appropriate part of the system, and it should be working now.

Still giving the same error.

I am sorry its working now.i had to upload my notebook again to make it work.

That's odd, I wouldn't have expected you to need to do that. Still, I'm glad it's working now. Will investigate a bit further to work out why you needed to do the extra step.