Question :python: [Errno 10054] An existing connection was forcibly closed by the remote host I am writing python to crawl Twitter space using Twitter-py. I have set the crawler to sleep for a while (2 seconds) between each request to api.twitter.com. However, after some times of running (around 1), when the Twitter’s rate limit not exceeded yet, I got this error. [Errno 10054] An existing connection was forcibly closed by the remote host.What are possible causes of this problem and how to solve this? I have searched through and found that the Twitter server itself may force to close the connection due to many requests. Thank you very much in advance. Answer #1:This can be caused by the two sides of the connection disagreeing over whether the connection timed out or not during a keepalive. (Your code tries to reused the connection just as the server is closing it because it has been idle for too long.) You should basically just retry the operation over a new connection. (I’m surprised your library doesn’t do this automatically.) Answer #2:there are many causes such as
To examine the problem in detail, you can use Wireshark. or you can just re-request or re-connect again. Answer #3:I know this is a very old question but it may be that you need to set the request headers. This solved it for me. For example ‘user-agent’, ‘accept’ etc. here is an example with user-agent: Answer #4:For me this problem arised while trying to connect to the SAP Hana database. When I got this error, OperationalError: Lost connection to HANA server (ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))I tried to run the code for connection(mentioned below), which created that error, again and it worked. import pyhdb connection = pyhdb.connect(host="example.com",port=30015,user="user",password="secret") cursor = connection.cursor() cursor.execute("SELECT 'Hello Python World' FROM DUMMY") cursor.fetchone() connection.close()It was because the server refused to connect. It might require you to wait for a while and try again. Try closing the Hana Studio by logging off and then logging in again. Keep running the code for a number of times. Your code works as expected, except that the response bytes should be decoded because json.loads expects a str object. UPDATE You need to configure proxy in Python code like the browser. You can use urllib.request.Request.set_proxy, or 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) I would use requests and make your code much simpler: 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://icons.wxug.com/graphics/wu2/logo_130x80.png, title: Weather Underground}, wind_gust_mph: 4.9, icon_url: http://icons.wxug.com/i/c/k/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: Berrys 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}}}json – Python 3 , Windows 7 :ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host |