Python UDP客户端没有响应
Python UDP client did not respond
一个python UDP客户端:
root@kali-linux:~# python
Python 2.7.14 (default, Sep 17 2017, 18:50:44)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> target_host = "127.0.0.1"
>>> target_port = 80
>>> client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> client.sendto("AAABBBCCC",(target_host,target_port))
9
>>> data, addr = client.recvfrom(4096)
# to this line,no response
>>>print data # waiting for imput
当我输入"data, addr = client.recvfrom(4096)"然后回车,我等了十分钟没有反应。
但是当我编写 TCP 客户端程序时,它 work.This 是代码:
root@kali-linux:~# python
Python 2.7.14 (default, Sep 17 2017, 18:55:37)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import socket
>>>target_host = "www.baidu.com"
>>>target_port = 80
>>>client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>>client.connect((target_host,target_port))
>>>client.send("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n")
35
>>>response = client.recv(4096)
>>>print response
#output
HTTP/1.1 200 OK
Date: Thu, 16 Nov 2017 12:05:07 GMT
Content-Type: text/html
Content-Length: 14613
Last-Modified: Wed, 01 Nov 2017 03:00:00 GMT
Connection: Keep-Alive
Vary: Accept-Encoding
AND SO ON ..........
TCP客户端可以,UDP客户端不能,为什么?
我该怎么办?
UDP 端口和 TCP 端口是不同的实体。您可以在 TCP 端口 80 上有一个 TCP 服务器 运行ning,在 UDP 端口 80 上有一个完全不同的东西(例如 NTP 服务器,尽管这通常是在 UDP 端口 123 上 运行ning)。
在您的第一个代码片段中,您向 UDP 端口 80 发送了一个 UDP 数据包。很可能没有任何东西在 UDP 端口 80 上侦听,因此数据包被静默丢弃。
您的行 data, addr = client.recvfrom(4096)
正在等待 某人(不一定是您向其发送请求的服务器)向您发送另一个 UDP 数据包。因为通常没有任何东西在 UDP 端口 80 上监听,所以没有任何事情发生,并且 revcfrom
调用永远等待。它工作正常。这是意料之中的。
你可以做些什么来获得一些东西:
1. bind() 你的 UDP 片段套接字到端口 2222(或任何空闲端口),然后 运行 它。它将再次永远等待。
2. 运行 第二个实例(在不同的 shell 中)带有 `target_port = 2222'。现在您应该在第一个实例中收到内容为 "AAABBBCCC" 的 UDP 数据包。
在您的第二个代码片段中,您使用的是 TCP,并且您正在联系响应的 TCP 端口 80(而非 UDP 端口 80)上的网络服务器。如您所见,这按预期工作,但只是因为 TCP 端口 80 上有一些东西 运行ning。
一个python UDP客户端:
root@kali-linux:~# python
Python 2.7.14 (default, Sep 17 2017, 18:50:44)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> target_host = "127.0.0.1"
>>> target_port = 80
>>> client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> client.sendto("AAABBBCCC",(target_host,target_port))
9
>>> data, addr = client.recvfrom(4096)
# to this line,no response
>>>print data # waiting for imput
当我输入"data, addr = client.recvfrom(4096)"然后回车,我等了十分钟没有反应。
但是当我编写 TCP 客户端程序时,它 work.This 是代码:
root@kali-linux:~# python
Python 2.7.14 (default, Sep 17 2017, 18:55:37)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import socket
>>>target_host = "www.baidu.com"
>>>target_port = 80
>>>client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>>client.connect((target_host,target_port))
>>>client.send("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n")
35
>>>response = client.recv(4096)
>>>print response
#output
HTTP/1.1 200 OK
Date: Thu, 16 Nov 2017 12:05:07 GMT
Content-Type: text/html
Content-Length: 14613
Last-Modified: Wed, 01 Nov 2017 03:00:00 GMT
Connection: Keep-Alive
Vary: Accept-Encoding
AND SO ON ..........
TCP客户端可以,UDP客户端不能,为什么?
我该怎么办?
UDP 端口和 TCP 端口是不同的实体。您可以在 TCP 端口 80 上有一个 TCP 服务器 运行ning,在 UDP 端口 80 上有一个完全不同的东西(例如 NTP 服务器,尽管这通常是在 UDP 端口 123 上 运行ning)。
在您的第一个代码片段中,您向 UDP 端口 80 发送了一个 UDP 数据包。很可能没有任何东西在 UDP 端口 80 上侦听,因此数据包被静默丢弃。
您的行 data, addr = client.recvfrom(4096)
正在等待 某人(不一定是您向其发送请求的服务器)向您发送另一个 UDP 数据包。因为通常没有任何东西在 UDP 端口 80 上监听,所以没有任何事情发生,并且 revcfrom
调用永远等待。它工作正常。这是意料之中的。
你可以做些什么来获得一些东西: 1. bind() 你的 UDP 片段套接字到端口 2222(或任何空闲端口),然后 运行 它。它将再次永远等待。 2. 运行 第二个实例(在不同的 shell 中)带有 `target_port = 2222'。现在您应该在第一个实例中收到内容为 "AAABBBCCC" 的 UDP 数据包。
在您的第二个代码片段中,您使用的是 TCP,并且您正在联系响应的 TCP 端口 80(而非 UDP 端口 80)上的网络服务器。如您所见,这按预期工作,但只是因为 TCP 端口 80 上有一些东西 运行ning。