2017-04-11 111 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