2014-11-02 115 views
3

我正在開發一些使用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) 

我想有一個解釋的錯誤意味着什麼,爲什麼發生這種情況。提前致謝。我非常感謝幫助。

回答

5

那麼,這是不是燒瓶錯誤,它基本上蟒蛇套接字錯誤

爲10060似乎是一個超時錯誤,是否有可能在服務器中接受,如果網站在瀏覽器中打開很慢, ,那麼你的瀏覽器有可能有更高的超時閾值?

嘗試增加請求時間在request.GET中()

如果遠程服務器也是在你的訪問,則: 你並不需要綁定套接字(除非遠程服務器有進入插座的預期) - 這實際上是連接的一個要求,這是非常罕見的。

+0

我正在使用本地服務器關閉我的電腦。如何在Python中增加請求時間? – 2014-11-02 16:53:31

+2

requests.get(「http://github.com」,timeout = 5) http://docs.python-requests.org/en/latest/user/quickstart/#timeouts – 2014-11-03 15:35:59

+0

謝謝,這有幫助! – 2014-11-04 01:07:32

2

我想這是因爲steamcommunity的服務器不穩定或者一些網絡問題..

通常情況下,你不能修復的淨shake.But那裏是仍然有一些方法可以幫助ü。

首先,當你發現網絡錯誤時,你讓你的線程睡眠一秒鐘,然後再試一次!其次,我建議你使用Flask-Cache,例如緩存60秒,這將有助於減少http請求。

+0

謝謝你的建議。我基本上是新的瓶,我怎麼會通過代碼來完成。 – 2014-11-02 16:45:49

0

根據我的經驗,那些「關聯方在一段時間後沒有迴應」,特別是當以前使用的相同代碼通常與MTU大小有關時。通常你應該只是determine the maximum MTU size(不要忘記添加28個字節,如鏈接中所述),然後你可以在你的客戶端計算機change the MTU size

相關問題