我想從我的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)
但是'broadcast_string(Sensor_IP,jsonString)'的Sensor_IP'和'jsonString''也沒有被定義... – Tangwz
@Tangwz我不確定這些是全局變量,但我希望它們是未定義的否則 –