简体   繁体   English

Python Twisted客户端无法从服务器接收响应

[英]Python Twisted client not able to receive response from server

I have a client written using python-twisted ( http://pastebin.com/X7UYYLWJ ) which sends a UDP packet to a UDP Server written in C using libuv. 我有一个使用python-twisted( http://pastebin.com/X7UYYLWJ )编写的客户端,该客户端将UDP数据包发送到使用libuv用C编写的UDP服务器。 When the client sends a packet to the server, it is successfully received by the server and it sends a response back to the python client. 当客户端将数据包发送到服务器时,服务器成功接收到数据包,并将响应发送回python客户端。 But the client not receiving any response, what could be the reason ? 但是客户没有收到任何答复,这可能是什么原因?

Unfortunately for you, there are many possibilities. 不幸的是,您有很多可能性。

Your code uses connect to set up a "connected UDP" socket. 您的代码使用connect来设置“已连接的UDP”套接字。 Connected UDP sockets filter the packets they receive. 连接的UDP套接字过滤接收到的数据包。 If packets are received from any address other than the one to which the socket is connected, they are dropped. 如果从与套接字连接的地址不同的任何地址接收到数据包,则将其丢弃。 It may be that the server sends its responses from a different address than you've connected to (perhaps it uses another port or perhaps it is multi-homed and uses a different IP). 服务器可能从与您连接的地址不同的地址发送响应(也许它使用另一个端口,或者它是多宿主的,并且使用了不同的IP)。

Another possibility is that a NAT device is blocking the return packets. 另一可能性是NAT设备阻止了返回数据包。 UDP NAT hole punching has come a long way but it's still not perfect. UDP NAT打孔已经走了很长一段路,但仍不完美。 It could be that the server's response arrives at the NAT device and gets discarded or misrouted. 服务器的响应可能到达NAT设备,并被丢弃或路由错误。

Related to this is the possibility that an intentionally configured firewall is blocking the return packets. 与此相关的是,故意配置的防火墙阻止了返回数据包的可能性。

Another possibility is that the packets are simply lost. 另一种可能性是分组只是丢失。 UDP is not a reliable protocol. UDP不是可靠的协议。 A congested router, faulty networking gear, or various other esoteric (often transient) concerns might be resulting in the packet getting dropped at some point, instead of forwarded to the next hop. 路由器拥塞,网络设备故障或各种其他深奥的(通常是瞬态的)问题都可能导致数据包在某个时刻丢失,而不是转发到下一跳。

Your first step in debugging this should be to make your application as permissive as possible. 调试的第一步应该是使您的应用程序尽可能宽松。 Get rid of the use of connected UDP so that all packets that make it to your process get delivered to your application code. 摆脱对已连接UDP的使用,以便使所有连接到您的进程的数据包都传递到您的应用程序代码。

If that doesn't help, use tcpdump or wireshark or a similar tool to determine if the packets make it to your computer at all. 如果那没有帮助,请使用tcpdump或wireshark或类似工具确定数据包是否完全进入了您的计算机。 If they do but your application isn't seeing them, look for a local firewall configuration that might reject them. 如果它们可以,但是您的应用程序看不到它们,请寻找可能会拒绝它们的本地防火墙配置。

If they're not making it to your computer, see if they make it to your router. 如果他们没有将其安装到您的计算机上,请查看是否将其安装到路由器上。 Use whatever diagnostic tools are available (along the lines of tcpdump) on your router to see whether packets make it that far or not. 使用路由器上可用的任何诊断工具(沿tcpdump行),以查看数据包是否达到了那么远。 Or if there are no such tools, remove the router from the equation. 或者,如果没有此类工具,则从公式中删除路由器。 If you see packets making it to your router but no further, look for firewall or NAT configuration issues there. 如果您看到将其发送到路由器的数据包,但没有更多,请在此处查找防火墙或NAT配置问题。

If packets don't make it as far as your router, move to the next hop you have access to. 如果数据包无法到达路由器,请移至您可以访问的下一跳。 This is where things might get difficult since you may not have access to the next hop or the next hop might be the server (with many intervening hops - which you have to just hope are all working). 在这里,事情可能会变得很困难,因为您可能无法访问下一跳,或者下一跳可能是服务器(中间有许多跳,您只能希望它们都在工作)。

Does the server actually generate a reply? 服务器实际上是否产生回复? What addressing information is on that reply? 该回复中包含哪些地址信息? Does it match the client's expectations? 是否符合客户的期望? Does it get dropped at the server's outgoing interface because of congestion or a firewall? 是否由于拥塞或防火墙而将其丢弃在服务器的传出接口上?

Hopefully you'll discover something interesting at one of these steps and be able to fix the problem. 希望您可以通过以下步骤之一发现一些有趣的东西,并且能够解决问题。

I had a similar problem. 我有一个类似的问题。 The problem was windows firewall. 问题是Windows防火墙。 In firewall allowed programs settings, allowing the communication for pythonw/python did solve the problem. 在防火墙允许的程序设置中,允许pythonw / python的通信确实解决了问题。 My python program was: 我的python程序是:

    from socket import *
    import time
    address = ( '192.168.1.104', 42) #Defind who you are talking to (must match arduino IP and port)
    client_socket = socket(AF_INET, SOCK_DGRAM) #Set Up the Socket
    client_socket.bind(('', 45)) # arduino sending to port 45
    client_socket.settimeout(1) #only wait 1 second for a response
    data = "xyz"
    client_socket.sendto(data, address)
    try:
            rec_data, addr = client_socket.recvfrom(2048) #Read response from arduino
            print rec_data #Print the response from Arduino
    except:
            pass
    while(1):
            pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM