2014-01-08 20 views
0

我有一些腳本在常規py文件來檢查連接或不在所需的URL。Flask用什麼API檢查某個遠程(其他)服務器的連接?

import urllib2 
import time 
global xUrl 
Url = 'http://stackoverflow.com/' 

def checkNetwork(): 
    try: 
    response=urllib2.urlopen(Url,timeout=1) 
     return True 
    except urllib2.URLError as err: pass 
    return False 

while 1: 
    if checkNetwork() == 1: 
     print "Connected to", Url 
    else: 
     print "Cannot Connected to", Url 

    time.sleep(5) 
    pass 

對於瓶中實現,我也試試這個:

@app.route('/status/') 
@auth.login_required 
def checkNetwork(): 
    user = auth.get_logged_in_user() 
    optiondetails = Optionplatform.select().where(Optionplatform.user==user.id) 
    xUrl = Optionplatform.remoteserver #i edited the posted questions. 

    try: 
     response = urllib2.urlopen(xUrl,timeout=1) 
     status = "Connected to " + xUrl 
     return render_template('checkNetwork.html', status) 
    except urllib2.URLError as err: 
     status = "NOT Connected to " + xUrl 
     return render_template('checkNetwork.html', status) 

    return render_template('checkNetwork.html') 

和模板,

<html> 
<head> </head> 
<body> 
{% block content_details %} 

<p>{{ status }} </p> 
{% endblock %} 
</body> 
</html> 

但得到這個錯誤: Error Messages 問題是,如何在實現燒瓶 ?應該使用Flask中的哪些API?而不是urllib32打開一些URI並檢查,連接或不連接到遠程服務器。

感謝您的任何建議。

編輯答案按/米格爾評論: 這裏的模型。

class Optionplatform(db.Model): 
    user = ForeignKeyField(User, related_name='user_option') 
    remoteserver = TextField() 
    token = TextField() 

並根據@miguel建議,之前我寫就像@miguel說:

xUrl = optiondetails.remoteserver 

但後來occure指示的錯誤消息,療法是在models.py沒有 「REMOTESERVER」。這裏的錯誤消息「AttributeError:'SelectQuery'對象沒有屬性'remoteserver'」。

other error messages

所以我改爲:

xUrl = Optionplatform.remoteserver 

和錯誤消息是在第一印象。

+1

很抱歉,但我有一個非常很難理解你正在嘗試做的事情。看起來好像您認爲Flask被用作Web客戶端(也就是說,您使用Flask將計算機連接到像StackOverflow這樣的網站)。它實際上是相反的:Flask意思是允許你接受連接:它可以用來創建一個像StackOverflow這樣的網站。 –

+0

不是那種方式。這是我的一個小實驗,它可以與其他Web服務(比如說用容器構建)或者其他框架(與其他框架一起構建)進行通信的小型物聯網(機器到機器)。 Fyi,我從這個想法(http://flask.pocoo.org/snippets/73/)中獲得啓發,並嘗試理解它,首先用上面的腳本進行一些測試。無論如何,謝謝馬克,您的評論。這有助於我理解這個頁面(http://flask.pocoo.org/docs/deploying/others/)。 – hepidad

回答

0

這裏有一個簡單的方法,使用優秀requests library

import requests 

@app.route('/status/') 
@auth.login_required 
def checkNetwork(): 
    user = auth.get_logged_in_user() 
    optiondetails = Optionplatform.select().where(Optionplatform.user==user.id) 
    xUrl = optiondetails.remoteserver #i edited the posted questions. 

    status = None 
    try: 
     r = requests.get(xUrl) 
    except requests.exceptions.ConnectionError: 
     status = "Network Error while connecting to {}".format(xUrl) 

    if r and str(r.status_code)[0] not in ('2','3'): 
     status = "URL {} returned {}".format(xUrl, status_code) 
    else: 
     status = "Connection established to {}".format(xUrl) 

    return render_template('checkNetwork.html', status=status) 
+0

Bingo ..,對於請求 - lib,看起來像人類。無論如何,我認爲 – hepidad

+0

我手動添加var xURL = some.ip.address(以測試requests-lib),因爲select()問題尚未修復。這是作品!看起來像request-lib更加人性化。 無論如何,我認爲render_template('checkNetwork.html',狀態),只允許只有一個參數(TypeError:render_template()只需要1個參數(給出2))。爲了解決這個問題,我改爲:render_template('checkNetwork.html',** locals())。 – hepidad

+1

你需要'render_template('checkNetwork.html',status = status)'和* not *'** locals()'。 –

0

您發送給urllib2xUrl參數需要是字符串(或urllib2.Request對象,但這似乎不是您想要的)。

從您的堆棧跟蹤似乎xUrl從燒瓶Peewee一個TextField對象,而不是。

你不顯示你的模型的定義,但我相信這是這是錯誤的代碼:我認爲

optiondetails = Option.select().where(Option.user==user.id) 
xUrl = Optionp.remoteserver 

,而不是你想要的:

optiondetails = Option.select().where(Option.user==user.id) 
xUrl = optiondetails.remoteserver 

或者相近,你知道你的模型,我不知道,所以你必須適應它。重要的是你確定你放入xUrl的是一個帶有URL的字符串。

+0

我更新了我的帖子,顯示了模型。 我錯了,而不是「Optionp」,但我的意思是Optionplatform,這意味着調用model.py。僅供參考,在我的代碼使用這個參數之前,(我相信這是正確的) xUrl = optiondetails.remoteserver 但後來發生的錯誤消息表明,它在models.py中不是「remoteserver」。這裏的錯誤消息「AttributeError:'SelectQuery'對象沒有屬性'remoteserver'」。我將在我發佈的問題中添加此錯誤的屏幕截圖。 I – hepidad