2015-03-18 74 views
0

下面的腳本可以成功地從單個IP中提取正確的信息(url_ip)。但在嘗試構建循環過程後,請求調用會因連接錯誤而失敗。 (以下錯誤) *注 - 馬虎代碼,所以要警告。Python在循環中執行請求函數調用?

from lxml import html 
import requests 
import smtplib 

# STATIC URL 
#TODO PULL A LIST OF IP ADDRESSES AND BUILD THE URL FOR EACH SYSTEM 

#IPs = ['192.168.3.152','192.168.3.194'] 

def crawler(url_ip): 
    global eqid, counter, serial 
    print "Starting Crawler Service for: " + url_ip 
    url = "http://" + url_ip + "/cgi-bin/dynamic/printer/config/reports/deviceinfo.html" 
    urleqid = "http://" + url_ip + "/cgi-bin/dynamic/topbar.html" 

    response = requests.get(url) 
    tree = html.fromstring(response.text) 

    counter = tree.xpath('//td[contains(p,"Count")]/following-sibling::td/p/text()') 
    serial = tree.xpath('//td[contains(p, "Serial")]/following-sibling::td/p/text()') 
    counter = counter[0].split(' ')[3] 
    serial = serial[0].split(' ')[3] 

    responseeqid = requests.get(urleqid) 
    treeequid = html.fromstring(responseeqid.text) 
    eqid = treeequid.xpath('//descendant-or-self::node()/child::b[contains(., "Location")]/text()')[1].split(' ')[-1] 

    print " -- equipment id found: " + eqid 
    print " -- count found: " + counter 
    print " -- serial found: " + serial 
    print "Stopping Crawler Service for: " + url_ip 

    return 

def send_mail(eqid,counter,serial): 
    GMAIL_USERNAME = "removed" 
    GMAIL_PASSWORD = "removed" 

    recipient = "removed" 
    email_subject = "Test" 
    body_of_email = "Equipment ID = " + eqid + "<br>Total Meter Count = " + counter + "<br>Serial Number = " + serial + "<br><br>" 

    session = smtplib.SMTP('smtp.gmail.com', 587) 
    session.ehlo() 
    session.starttls() 
    session.login(GMAIL_USERNAME, GMAIL_PASSWORD) 

    headers = "\r\n".join(["from: " + GMAIL_USERNAME, 
         "subject: " + email_subject, 
         "to: " + recipient, 
         "mime-version: 1.0", 
         "content-type: text/html"]) 

    # body_of_email can be plain text or html!      
    content = headers + "\r\n\r\n" + body_of_email 
    session.sendmail(GMAIL_USERNAME, recipient, content) 
    return 

with open('iplist.txt') as fp: 
    for line in fp: 
     crawler(line); 

#send_mail(eqid,counter,serial); 

錯誤日誌:

Starting Crawler Service for: 192.168.3.152 

Traceback (most recent call last): 
    File "getmeters.py", line 63, in <module> 
    crawler(ipstring); 
    File "getmeters.py", line 17, in crawler 
    response = requests.get(url) 
    File "/Library/Python/2.7/site-packages/requests/api.py", line 68, in get 
    return request('get', url, **kwargs) 
    File "/Library/Python/2.7/site-packages/requests/api.py", line 50, in request 
    response = session.request(method=method, url=url, **kwargs) 
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 464, in request 
    resp = self.send(prep, **send_kwargs) 
    File "/Library/Python/2.7/site-packages/requests/sessions.py", line 576, in send 
    r = adapter.send(request, **kwargs) 
    File "/Library/Python/2.7/site-packages/requests/adapters.py", line 415, in send 
    raise ConnectionError(err, request=request) 
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(8, 'nodename nor servname provided, or not known')) 

我認爲這是由於被處理作爲一個列表對象,而不是一個字符串值「線」,所以轉換到STR(線)和那也失敗了。

回答

1

我懷疑在文件中的行尾有行結束符(\ n),您可能需要將這些行結束。否則你的URL變得像

http://192.168.3.152 
/cgi-bin/dynamic/printer/config/reports/deviceinfo.html" 

而不是預期的

http://192.168.3.152/cgi-bin/dynamic/printer/config/reports/deviceinfo.html" 
+0

這甚至從來沒有穿過我的腦海......總是小事情。 – SudoGaron 2015-03-18 21:58:44