为什么我的基本 python UDP 客户端会收到 ConnectionResetError 错误?

问题描述

我在 VMWare Workstation 16 Player 上使用 2020.4 Kali Linux VM,我正在使用 Justin Seitz 的 Black Hat Python 书。在第 2 章的开头,他介绍了一个基本的 UDP 客户端,但由于某种原因,每次我都会抛出一个 ConnectionResetError,因为我发送到的端口或我接收的端口被占用。然后我添加了一行以使其绑定到我要发送到的地址并且它起作用了。 sendto() 的时候不是自动绑定的吗?如果我在进行渗透测试,当我制作 UDP 客户端时,我不需要密码/管理员来绑定。

这是我的代码

import socket


address = ('127.0.0.1',80)

# Create a socket object.
client = socket.socket(socket.AF_INET,socket.soCK_DGRAM)

# I commented this out just for testing reasons.
# client.bind(address)

# Send some data.
client.sendto(b'AAABBBCCC',address)

# Receive some data.
data,addr = client.recvfrom(4096)

print(data)

错误如下:

Traceback (most recent call last):
  File ".\udp_client.py",line 15,in <module>
    data,addr = client.recvfrom(4096)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

如果这是我的愚蠢错误,我提前道歉。

编辑:

我更改了代码以使用不同的端口 (65536) 但现在它只是不打印任何内容或结束脚本,它只是继续运行。

解决方法

我建议您尝试使用 80 端口以外的端口(所有 HTTP 流量的默认端口),该端口可能经常被使用并且不是一个可以尝试并坚持的好端口,请尝试使用端口号 > 1023