请大家帮我看下这代码有什么错,想要实现这样的功能“服务器上监听一个 UDP 端口,在收到 UDP 数据包后把源地址发回去”
脚本源码udpaddr.py :
import sysimport timeimport socket def main(port): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('', port)) try: while True: data, addr = s.recvfrom(4096) back = 'Your address is %r\n' % (addr,) s.sendto(back.encode(), addr) print(time.strftime('%Y-%m-%d %H:%M:%S'), addr, 'just sent us a message:', data.decode('utf-8', 'replace'), end='') except KeyboardInterrupt: print() if __name__ == '__main__': try: main(int(sys.argv[1])) except (ValueError, IndexError): sys.exit('which port to listen?')
line@Excalibur:~$ python udpaddr.py File "udpaddr.py", line 13 print(time.strftime('%Y-%m-%d %H:%M:%S'), addr, 'just sent us a message:', data.decode('utf-8', 'replace'), end='') ^SyntaxError: invalid syntax