2017-04-11 164 views
0

我想從我的scan_db()返回通過它返回到下一個函數json_create(響應)並使用從我的json_create(響應)返回傳遞給我的broadcast_string(Sensor_IP ,jsonString)。函數未定義Python的

每次我嘗試運行該程序時,都會收到錯誤「NameError:name'響應'未定義」我無法在任何地方找到有用的示例。

我使用Python 2.7

mycursor = conn.cursor() 


def scan_db(): 
    print "Scanning DB" 
    mycursor.execute("SELECT * FROM outputs ORDER BY ID DESC LIMIT 1;") 
    response = mycursor.fetchall() 

    return response 


def json_create(response): 
    ID, Sensor_ID, Sensor_IP, State, Pending_Update, TimeStamp = response[0] 

    print "Json string creating" 
    print "" 
    print "ID", ID 
    print "Sensor Name", Sensor_ID 
    print "Sensor IP", Sensor_IP 
    print "State", State 
    print "Pending update", Pending_Update 
    print "Time", TimeStamp 
    print "" 

    jsonSwitch = {'Sensor_Name': Sensor_ID, 'Sensor_IP': Sensor_IP, 'State': State, 
       'Pending_Update': Pending_Update} 

    jsonString = json.dumps(jsonSwitch) 

    return jsonString, Sensor_IP 


def broadcast_string(Sensor_IP, jsonString): 
    UDP_IP = Sensor_IP # broadcast 
    UDP_PORT = 5002 

    print "UDP target IP:", UDP_IP 
    print "UDP target port:", UDP_PORT 
    print "message:", jsonString 

    cs = socket(AF_INET, SOCK_DGRAM) 
    cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) 
    cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) 
    cs.sendto(jsonString, (UDP_IP, UDP_PORT)) 


while True: 
    print "while loop started" 
    scan_db() 
    print "scanning db" 
    json_create(response) 
    print "creating Json string" 
    broadcast_string(Sensor_IP, jsonString) 
    print "broadcasting packet" 
    time.sleep(2) 

回答

0

您還沒有存儲其結果scan_db()了回報,在json_create(response)response是什麼。嘗試創建一個變量response及其內容相等的值設置爲scan_db()回報,像這樣:

while True: 
    print "while loop started" 
    response = scan_db() # edit this line 
    print "scanning db" 
    json_create(response) 
    print "creating Json string" 
    broadcast_string(Sensor_IP, jsonString) 
    print "broadcasting packet" 
    time.sleep(2) 

同樣,你也可以做json_create(scan_db())

+1

但是'broadcast_string(Sensor_IP,jsonString)'的Sensor_IP'和'jsonString''也沒有被定義... – Tangwz

+0

@Tangwz我不確定這些是全局變量,但我希望它們是未定義的否則 –

0

,就應該替換這段代碼:

while True: 
    print "while loop started" 
    scan_db() 
    print "scanning db" 
    json_create(response) 
    # the rest of your code 
    # ... 

通過

while True: 
    print "while loop started" 
    # create a new response variable 
    response = scan_db() 
    print "scanning db" 
    json_create(response) 
    # the rest of your code 
    # .... 

解說:

您的scan_db()方法將返回您將在json_create(response)中使用的response變量。

0

在while循環,你應該

while True: 
    print "while loop started" 
    response = scan_db() 
    print "scanning db" 
    jsonString, Sensor_IP = json_create(response) 
    print "creating Json string" 
    broadcast_string(Sensor_IP, jsonString) 
    print "broadcasting packet" 
    time.sleep(2) 

也許你應該知道什麼是local variable

+0

我現在得到NameError:名稱'Sensor_IP'未定義 –

+0

@ moe.Patel我編輯答案,您應該得到函數'json_create'的返回值,並將返回變量傳遞給下一個函數。 – Tangwz