I’m trying to set up a UDP server with Twisted folliowing this http://twistedmatrix.com/documents/current/core/howto/udp.html
However, I’ve hit a brickwall just starting. I tried this sample code:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
class Echo(DatagramProtocol):
def datagramReceived(self, data, (host, port)):
print "received %r from %s:%d" % (data, host, port)
self.transport.write(data, (host, port))
reactor.listenUDP(9999, Echo())
reactor.run()
And I get this:
def datagramReceived(self, data, (host, port)):
^
SyntaxError: invalid syntax
I’m new to Python so I’m clueless. I stripped the code down to the minimum, commenting everything but the class declaration and the method header (adding a pass) but I get the same. Are those paired parameters not supported anymore?
Are you sure it’s python 2.7. Because PEP 3113 — Removal of Tuple Parameter Unpacking describes the removal of that syntax in Python 3. As a test when I run the below dummy function in python 2.7 it works. In Python 3.2 it gives me your exact same error:
python 3 error:
try this in your code just to be sure of your python version: