I'm new with Python and I've encountered this error this afternoon. I tried to fix this by adding global before the previous variable but I continue to get this error:
Traceback (most recent call last):
File "send.py", line 76, in <module>
main(sys.argv[1:])
File "send.py", line 34, in main
send()
File "send.py", line 29, in send
if data != previous:
A sample of the code I did:
import socket
import sys
import getopt
import time
import threading
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
def main(argv):
global bridge
global previous
try:
# Create a UDP socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.1.100', 9050)
bridge = bridgeclient()
previous = ""
# Send data
def send():
data = bridge.get("data")
if data != previous:
sent = sock.sendto(data, server_address)
previous = data
threading.Timer(0.2, send).start()
send()
finally:
sock.close()
if __name__ == "__main__":
main(sys.argv[1:])
BridgeClientimport is not likely related but makes it so we can't test your code. Post something smaller that demonstrates the issue. You could copy this file and delete half of it including the socket calls.globaland don't use nested functions.