我正在開發一些使用Steam API的python軟件。我使用Flask來運行和測試python代碼。一切都進行得膨脹,但現在我得到這個錯誤(我沒有改變任何代碼):連接錯誤:連接嘗試失敗,因爲連接方在一段時間後沒有正確響應
('Connection aborted.', error(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
我不知道爲什麼這個錯誤就要到了,由於代碼工作完全正常,突然錯誤出現了,我沒有改變任何代碼或者我的電腦或者Flask。
代碼:
import urllib
import itertools
import urllib2
import time
from datetime import datetime
from bs4 import BeautifulSoup
from flask import Flask
import requests
import json
import xml.etree.ElementTree as ET
from xml.dom.minidom import parseString
import sys
app = Flask(__name__)
API_KEY = 'XXX'
API_BASEURL = 'http://api.steampowered.com/'
API_GET_FRIENDS = API_BASEURL + 'ISteamUser/GetFriendList/v0001/?key='+API_KEY+'&steamid='
API_GET_SUMMARIES = API_BASEURL + 'ISteamUser/GetPlayerSummaries/v0002/?key='+API_KEY+'&steamids='
PROFILE_URL = 'http://steamcommunity.com/profiles/'
steamIDs = []
myFriends = []
class steamUser:
def __init__(self, name, steamid, isPhisher):
self.name = name
self.steamid = steamid
self.isPhisher = isPhisher
def setPhisherStatus(self, phisher):
self.isPhisher = phisher
@app.route('/DeterminePhisher/<steamid>')
def getFriendList(steamid):
try:
r = requests.get(API_GET_FRIENDS+steamid)
data = r.json()
for friend in data['friendslist']['friends']:
steamIDs.append(friend['steamid'])
return isPhisher(steamIDs)
except requests.exceptions.ConnectionError as e:
return str(e.message)
def isPhisher(ids):
phisherusers = ''
for l in chunksiter(ids, 50):
sids = ','.join(map(str, l))
try:
r = requests.get(API_GET_SUMMARIES+sids)
data = r.json();
for i in range(len(data['response']['players'])):
steamFriend = data['response']['players'][i]
n = steamUser(steamFriend['personaname'], steamFriend['steamid'], False)
if steamFriend['communityvisibilitystate'] and not steamFriend['personastate']:
url = PROFILE_URL+steamFriend['steamid']+'?xml=1'
dat = requests.get(url)
if 'profilestate' not in steamFriend:
n.setPhisherStatus(True);
phisherusers = (phisherusers + ('%s is a phisher' % n.name) + ', ')
if parseString(dat.text.encode('utf8')).getElementsByTagName('privacyState'):
privacy = str(parseString(dat.text.encode('utf-8')).getElementsByTagName('privacyState')[0].firstChild.wholeText)
if (privacy == 'private'):
n.setPhisherStatus(True)
phisherusers = (phisherusers + ('%s is a phisher' % n.name) + ', ')
elif 'profilestate' not in steamFriend:
n.setPhisherStatus(True);
phisherusers = (phisherusers + ('%s is a phisher' % n.name) + ', ')
else:
steamprofile = BeautifulSoup(urllib.urlopen(PROFILE_URL+steamFriend['steamid']).read())
for row in steamprofile('div', {'class': 'commentthread_comment '}):
comment = row.find_all('div', 'commentthread_comment_text')[0].get_text().lower()
if ('phisher' in comment) or ('scammer' in comment):
n.setPhisherStatus(True)
phisherusers = (phisherusers + ('%s is a phisher' % n.name) + ', ')
myFriends.append(n);
except requests.exceptions.ConnectionError as e:
return str(e.message)
except:
return "Unexpected error:", sys.exc_info()[0]
return phisherusers
def chunksiter(l, chunks):
i,j,n = 0,0,0
rl = []
while n < len(l)/chunks:
rl.append(l[i:j+chunks])
i+=chunks
j+=j+chunks
n+=1
return iter(rl)
app.run(debug=True)
我想有一個解釋的錯誤意味着什麼,爲什麼發生這種情況。提前致謝。我非常感謝幫助。
我正在使用本地服務器關閉我的電腦。如何在Python中增加請求時間? – 2014-11-02 16:53:31
requests.get(「http://github.com」,timeout = 5) http://docs.python-requests.org/en/latest/user/quickstart/#timeouts – 2014-11-03 15:35:59
謝謝,這有幫助! – 2014-11-04 01:07:32