我是python新手,目前我正在開發一款GPS跟蹤器,並使用Arduino Uno與Google地圖進行交互。我得到這個錯誤,它不讓我爲我的tcpServer運行.py腳本,這是整個腳本。NameError:未定義全局名稱'message'
#!的/ usr /斌/包膜蟒蛇
import socket
import MySQLdb
TCP_IP = 'my machine IP'
TCP_PORT = 32000
BUFFER_SIZE = 40
# ClearDB. Deletes the entire tracking table
def ClearDB(curs,d):
curs.execute ("""
INSERT INTO gmaptracker (lat, lon)
VALUES (0.0,0.0)""")
d.commit()
# Connect to the mySQL Database
def tServer():
try:
db = MySQLdb.connect (host = "my host",
user = "my user",
passwd = "my password",
db = "gmap")
except MySQLdb.Error, e:
print "Error %d: %s" %(e.args[0], e.args[1])
sys.exit(1);
cursor = db.cursor()
# Start with a fresh tracking table
ClearDB(cursor,db)
# Set up listening Socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
print "Listening...."
s.listen(1)
conn, addr = s.accept()
print 'Accepted connection from address:', addr
except socket.error (message):
if s:
s.close()
print "Could not open socket: " + message
cursor.close()
conn.close()
db.close()
sys.exit(1)
try:
while 1:
data = conn.recv(BUFFER_SIZE)
if not data:break
str1,str2 = data.split("Long: ")
str1 = str1.split("Lat: ")[1]
latitude = float(str1)
longitude = float(str2)
cursor.execute ("""
INSERT INTO gmaptracker (lat, lon)
VALUES (%s,%s)""", (latitude,longitude))
db.commit()
except KeyboardInterrupt:
ClearDB(cursor,db);
cursor.close()
conn.close()
db.close()
if __name__ == '__main__':
tServer()
,這是我得到
Traceback (most recent call last):
File "tcpServer.py", line 79, in <module>
tServer()
File "tcpServer.py", line 48, in tServer
except socket.error(message):
NameError: global name 'message' is not defined
如果有人可以幫助我弄清楚了這一點,我將不勝感激錯誤,正如我所說我是新的python我也運行python 2.7如果有幫助。在此先感謝
感謝那些幫助,但現在我得到這樣的:回溯(最後最近一次調用):文件「tcpServer.py」,line80 ,在 tServer()文件「tcpServer.py」,第54行,在tServer中conn.close() –
您試圖關閉'conn',但是在設置變量之前拋出了'socket.error'。 –
,但變量之前設置除... –