無效語法我想建立一個UDP服務器扭folliowing這個http://twistedmatrix.com/documents/current/core/howto/udp.html的Python的高清
不過,我已經打了一個磚牆剛剛開始。我試過此示例代碼:
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()
而且我得到這個:
def datagramReceived(self, data, (host, port)):
^
SyntaxError: invalid syntax
我是新來的Python,所以我無言以對。我將代碼剝離到了最低限度,除了類聲明和方法頭部(添加了一個通道)之外的所有內容都被註釋掉了,但我得到了相同的結果。那些配對參數不再被支持嗎?
什麼您使用的是Python版本?另外,你確定沒有縮進錯誤嗎? – BrenBarn
2.7,每個縮進有4個空格。 – broncoAbierto
該語法在Python 3中已被刪除,但仍可在Python 2.7中使用。您是否確信:a)代碼是在Python 2.7中運行的(也就是說,您是否還安裝了可能正在運行的Python 3); b)您粘貼的代碼恰好是您正在運行的代碼? – BrenBarn