Python 3, Windows 7: ConnectionResetError: El host remoto cerró forzosamente una conexión existente

Editado:

Yo uso env corporativo, en mi casa env el script funciona bien

Estoy tratando de obtener respuesta de algún sitio del clima. Hice un simple script en python.

import urllib.request import json req=urllib.request.Request("http://api.wunderground.com/api/e0b319c6f7e8115a/geolookup/conditions/q/IA/Cedar_Rapids.json") response=urllib.request.urlopen(req) data = json.loads(response.read().decode()) #<--according to @falsetru fix print (data) 

Cuando pego la URL en el navegador, recibo respuesta, pero en Python me sale un error, por favor ayuda

Error completo

 Traceback (most recent call last): File "C:\Python34\weather.py", line 14, in  response=urllib.request.urlopen(req) File "C:\Python34\lib\urllib\request.py", line 153, in urlopen return opener.open(url, data, timeout) File "C:\Python34\lib\urllib\request.py", line 455, in open response = self._open(req, data) File "C:\Python34\lib\urllib\request.py", line 473, in _open '_open', req) File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain result = func(*args) File "C:\Python34\lib\urllib\request.py", line 1202, in http_open return self.do_open(http.client.HTTPConnection, req) File "C:\Python34\lib\urllib\request.py", line 1177, in do_open r = h.getresponse() File "C:\Python34\lib\http\client.py", line 1172, in getresponse response.begin() File "C:\Python34\lib\http\client.py", line 351, in begin version, status, reason = self._read_status() File "C:\Python34\lib\http\client.py", line 313, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Python34\lib\socket.py", line 371, in readinto return self._sock.recv_into(b) ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host 

Gracias

Su código funciona como se espera, excepto que los bytes de respuesta deben decodificarse porque json.loads espera un objeto str .

ACTUALIZAR

Necesitas configurar el proxy en el código Python como el navegador. Puede usar urllib.request.Request.set_proxy , o urllib.request.ProxyHandler :


 import urllib.request import json req = urllib.request.Request("http://api.wunderground.com/api/e0b319c6f7e8115a/geolookup/conditions/q/IA/Cedar_Rapids.json") req.set_proxy('PROXY_HOST:PROXY_PORT', 'http') # <--- response = urllib.request.urlopen(req) data = json.loads(response.read().decode()) # <---- print(data) 

Usaría peticiones y haría su código mucho más simple:

 import requests proxies = { "http": "http://ip:port", "https": "http://ip:port", } req = requests.get("http://api.wunderground.com/api/e0b319c6f7e8115a/geolookup/conditions/q/IA/Cedar_Rapids.json",proxies=proxies) data = req.json() print(data) {'current_observation': {'weather': 'Clear', 'wind_string': 'From the WSW at 4.6 MPH Gusting to 4.9 MPH', 'heat_index_string': 'NA', 'precip_1hr_in': '0.00', 'local_epoch': '1420127354', 'feelslike_string': '15 F (-9 C)', 'wind_degrees': 248, 'observation_epoch': '1420127338', 'local_tz_offset': '-0600', 'feelslike_c': '-9', 'UV': '0.0', 'visibility_km': '16.1', 'visibility_mi': '10.0', 'wind_kph': 7.4, 'ob_url': 'http://www.wunderground.com/cgi-bin/findweather/getForecast?query=41.981174,-91.682632', 'temp_f': 21.7, 'wind_gust_kph': '7.9', 'icon': 'clear', 'heat_index_c': 'NA', 'local_tz_short': 'CST', 'observation_time': 'Last Updated on January 1, 9:48 AM CST', 'pressure_mb': '1019', 'image': {'link': 'http://www.wunderground.com', 'url': 'http://sofes.miximages.com/python/logo_130x80.png', 'title': 'Weather Underground'}, 'wind_gust_mph': '4.9', 'icon_url': 'http://sofes.miximages.com/python/clear.gif', 'temperature_string': '21.7 F (-5.7 C)', 'dewpoint_f': 15, 'pressure_trend': '0', 'display_location': {'city': 'Cedar Rapids', 'latitude': '41.97171021', 'country': 'US', 'zip': '52401', 'wmo': '99999', 'country_iso3166': 'US', 'longitude': '-91.65871429', 'elevation': '223.00000000', 'magic': '1', 'state': 'IA', 'full': 'Cedar Rapids, IA', 'state_name': 'Iowa'}, 'station_id': 'KIACEDAR22', 'heat_index_f': 'NA', 'forecast_url': 'http://www.wunderground.com/US/IA/Cedar_Rapids.html', 'dewpoint_string': '15 F (-9 C)', 'pressure_in': '30.09', 'wind_dir': 'WSW', 'precip_today_metric': '0', 'windchill_f': '15', 'wind_mph': 4.6, 'observation_time_rfc822': 'Thu, 01 Jan 2015 09:48:58 -0600', 'precip_1hr_metric': ' 0', 'local_tz_long': 'America/Chicago', 'windchill_string': '15 F (-9 C)', 'local_time_rfc822': 'Thu, 01 Jan 2015 09:49:14 -0600', 'dewpoint_c': -9, 'solarradiation': '0', 'relative_humidity': '75%', 'nowcast': '', 'observation_location': {'country_iso3166': 'US', 'longitude': '-91.682632', 'city': 'Ellis Park Time Check, Cedar Rapids', 'latitude': '41.981174', 'elevation': '728 ft', 'state': 'Iowa', 'country': 'US', 'full': 'Ellis Park Time Check, Cedar Rapids, Iowa'}, 'estimated': {}, 'history_url': 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KIACEDAR22', 'precip_today_string': '0.00 in (0 mm)', 'feelslike_f': '15', 'precip_today_in': '0.00', 'windchill_c': '-9', 'temp_c': -5.7, 'precip_1hr_string': '0.00 in ( 0 mm)'}, 'location': {'tz_long': 'America/Chicago', 'type': 'CITY', 'city': 'Cedar Rapids', 'country': 'US', 'requesturl': 'US/IA/Cedar_Rapids.html', 'nearby_weather_stations': {'pws': {'station': [{'id': 'KIACEDAR22', 'neighborhood': 'Ellis Park Time Check', 'city': 'Cedar Rapids', 'distance_mi': 1, 'state': 'IA', 'country': 'US', 'distance_km': 2, 'lon': -91.682632, 'lat': 41.981174}, {'id': 'KIACEDAR41', 'neighborhood': 'PUBLIC HEALTH', 'city': 'Cedar Rapids', 'distance_mi': 1, 'state': 'IA', 'country': 'US', 'distance_km': 2, 'lon': -91.687798, 'lat': 41.9767}, {'id': 'KIACEDAR28', 'neighborhood': 'Indian Hill Rd', 'city': 'Cedar Rapids', 'distance_mi': 2, 'state': 'IA', 'country': 'US', 'distance_km': 4, 'lon': -91.617867, 'lat': 42.001289}, {'id': 'KIACEDAR35', 'neighborhood': 'NorthEast Cedar Rapids', 'city': 'Cedar Rapids', 'distance_mi': 2, 'state': 'IA', 'country': 'US', 'distance_km': 4, 'lon': -91.670929, 'lat': 42.014057}, {'id': 'MUP022', 'neighborhood': 'MesoWest OTIS1 IA US UPR', 'city': 'Cedar Rapids', 'distance_mi': 3, 'state': 'IA', 'country': 'US', 'distance_km': 5, 'lon': -91.592079, 'lat': 41.961639}, {'id': 'KIACEDAR46', 'neighborhood': 'Wolf River Lane Northwest', 'city': 'Cedar Rapids', 'distance_mi': 3, 'state': 'IA', 'country': 'US', 'distance_km': 5, 'lon': -91.722778, 'lat': 41.991871}, {'id': 'KIACEDAR37', 'neighborhood': 'College Farms', 'city': 'Cedar Rapids', 'distance_mi': 3, 'state': 'IA', 'country': 'US', 'distance_km': 6, 'lon': -91.616905, 'lat': 41.927353}, {'id': 'KIACEDAR47', 'neighborhood': 'Twin Pines', 'city': 'Cedar Rapids', 'distance_mi': 4, 'state': 'IA', 'country': 'US', 'distance_km': 6, 'lon': -91.694054, 'lat': 42.028618}, {'id': 'KIAMARIO18', 'neighborhood': 'Marion, IA south side', 'city': 'Marion', 'distance_mi': 4, 'state': 'IA', 'country': 'US', 'distance_km': 7, 'lon': -91.596588, 'lat': 42.026505}, {'id': 'MD2681', 'neighborhood': 'APRSWXNET Cedar Rapids IA US', 'city': 'Hiawatha', 'distance_mi': 4, 'state': 'IA', 'country': 'US', 'distance_km': 8, 'lon': -91.649834, 'lat': 42.0453}, {'id': 'KIACEDAR39', 'neighborhood': 'Oak Valley - Deer View Rd', 'city': 'Cedar Rapids', 'distance_mi': 5, 'state': 'IA', 'country': 'US', 'distance_km': 9, 'lon': -91.725883, 'lat': 42.035496}, {'id': 'KIACEDAR25', 'neighborhood': 'The Howling Wolves', 'city': 'Cedar Rapids', 'distance_mi': 5, 'state': 'IA', 'country': 'US', 'distance_km': 9, 'lon': -91.655243, 'lat': 42.053856}, {'id': 'KIAMARIO13', 'neighborhood': 'Deer Valley', 'city': 'Marion', 'distance_mi': 5, 'state': 'IA', 'country': 'US', 'distance_km': 9, 'lon': -91.567619, 'lat': 42.023415}, {'id': 'KIAMARIO6', 'neighborhood': 'Pheasant Ridge Ct', 'city': 'Marion', 'distance_mi': 5, 'state': 'IA', 'country': 'US', 'distance_km': 9, 'lon': -91.601463, 'lat': 42.049641}, {'id': 'MRCRI4', 'neighborhood': 'IADOT Cedar Rapids (US30)', 'city': 'Ely', 'distance_mi': 6, 'state': 'IA', 'country': 'US', 'distance_km': 10, 'lon': -91.549896, 'lat': 41.9259}, {'id': 'KIAMARIO15', 'neighborhood': 'Windemere 2nd Addition', 'city': 'Marion', 'distance_mi': 6, 'state': 'IA', 'country': 'US', 'distance_km': 11, 'lon': -91.572784, 'lat': 42.047874}, {'id': 'KIAMARIO17', 'neighborhood': 'Edgebrook Estates', 'city': 'Marion', 'distance_mi': 6, 'state': 'IA', 'country': 'US', 'distance_km': 11, 'lon': -91.58535, 'lat': 42.055588}, {'id': 'KIAMARIO8', 'neighborhood': '', 'city': 'Marion', 'distance_mi': 6, 'state': 'IA', 'country': 'US', 'distance_km': 11, 'lon': -91.559654, 'lat': 42.041698}, {'id': 'KIAMARIO19', 'neighborhood': 'Hunters Field', 'city': 'Marion', 'distance_mi': 7, 'state': 'IA', 'country': 'US', 'distance_km': 12, 'lon': -91.592049, 'lat': 42.068359}, {'id': 'KIAMARIO14', 'neighborhood': "Berry's Run", 'city': 'Marion', 'distance_mi': 7, 'state': 'IA', 'country': 'US', 'distance_km': 12, 'lon': -91.597679, 'lat': 42.077976}, {'id': 'KIASWISH5', 'neighborhood': 'Swisher', 'city': 'Swisher', 'distance_mi': 8, 'state': 'IA', 'country': 'US', 'distance_km': 14, 'lon': -91.69252, 'lat': 41.847839}, {'id': 'KIATODDV2', 'neighborhood': 'Midway, IA', 'city': 'Toddville', 'distance_mi': 9, 'state': 'IA', 'country': 'US', 'distance_km': 15, 'lon': -91.700241, 'lat': 42.105621}, {'id': 'KIAATKIN3', 'neighborhood': 'Ridgeview Estates', 'city': 'Atkins', 'distance_mi': 9, 'state': 'IA', 'country': 'US', 'distance_km': 16, 'lon': -91.853317, 'lat': 41.988167}, {'id': 'KIASPRIN1', 'neighborhood': 'Whittier', 'city': 'Springville', 'distance_mi': 12, 'state': 'IA', 'country': 'US', 'distance_km': 20, 'lon': -91.461136, 'lat': 42.082436}, {'id': 'KIALISBO2', 'neighborhood': '', 'city': 'Lisbon', 'distance_mi': 13, 'state': 'IA', 'country': 'US', 'distance_km': 22, 'lon': -91.393059, 'lat': 41.920174}, {'id': 'KIANORTH5', 'neighborhood': 'Cedar Springs - North Liberty', 'city': 'North Liberty', 'distance_mi': 14, 'state': 'IA', 'country': 'US', 'distance_km': 23, 'lon': -91.595421, 'lat': 41.767082}, {'id': 'KIAOXFOR4', 'neighborhood': 'Green Castle', 'city': 'Oxford', 'distance_mi': 14, 'state': 'IA', 'country': 'US', 'distance_km': 24, 'lon': -91.718925, 'lat': 41.753613}, {'id': 'KIACENTR2', 'neighborhood': '', 'city': 'Central City', 'distance_mi': 15, 'state': 'IA', 'country': 'US', 'distance_km': 25, 'lon': -91.654335, 'lat': 42.198334}]}, 'airport': {'station': [{'city': 'Cedar Rapids', 'state': 'IA', 'country': 'US', 'lon': '-91.70769501', 'icao': 'KCID', 'lat': '41.88240051'}, {'city': 'Iowa City', 'state': 'IA', 'country': 'US', 'lon': '-91.54306030', 'icao': 'KIOW', 'lat': '41.63277817'}, {'city': 'Vinton', 'state': 'IA', 'country': 'US', 'lon': '-92.02583313', 'icao': 'KVTI', 'lat': '42.21860886'}, {'city': 'Monticello', 'state': 'IA', 'country': 'US', 'lon': '-91.16329956', 'icao': 'KMXO', 'lat': '42.22036362'}]}}, 'zip': '52401', 'wmo': '99999', 'country_iso3166': 'US', 'country_name': 'USA', 'wuiurl': 'http://www.wunderground.com/US/IA/Cedar_Rapids.html', 'magic': '1', 'state': 'IA', 'tz_short': 'CST', 'l': '/q/zmw:52401.1.99999', 'lat': '41.97171021', 'lon': '-91.65871429'}, 'response': {'termsofService': 'http://www.wunderground.com/weather/api/d/terms.html', 'version': '0.1', 'features': {'conditions': 1, 'geolookup': 1}}}