gpt4 book ai didi

python - 试图做简单的 UDP 服务器客户端时 Argparse 错误

转载 作者:行者123 更新时间:2023-12-01 04:20:13 25 4
gpt4 key购买 nike

我一直在尝试此代码并从控制台获取此消息:

usage: Experimental.py [-h] [-p PORT] {client,server}
Experimental.py: error: the following arguments are required: role

我无法确定出了什么问题,这是代码:

    import argparse, socket
from datetime import datetime
MAX_BYTES = 65535
def server(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('127.0.0.1', port))
print('Listening at {}'.format(sock.getsockname()))
while True:
data, address = sock.recvfrom(MAX_BYTES)
text = data.decode('ascii')
print('The client at {} says {!r}'.format(address, text))
text = 'Your data was {} bytes long'.format(len(data))
data = text.encode('ascii')
sock.sendto(data, address)
def client(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
text = 'The time is {}'.format(datetime.now())
data = text.encode('ascii')
sock.sendto(data, ('127.0.0.1', port))
print('The OS assigned me the address{}'.format(sock.getsockname()))
data, address = sock.recvfrom(MAX_BYTES) # Danger!
text = data.decode('ascii')
print('The server {} replied {!r}'.format(address, text))


if __name__ == '__main__':
choices = {'client': client, 'server': server}
parser = argparse.ArgumentParser(description='Send and receive UDP locally')
parser.add_argument('role', choices=choices, help='which role to play')
parser.add_argument('-p', metavar='PORT', type=int, default=1060, help='UDP port (default 1060)')
args = parser.parse_args()
function = choices[args.role]
function(args.p)

最佳答案

该程序正在使用 argparse moduleArgumentParser 类,其中 assumes by default that positional arguments are required ,所以你不能像现在这样省略它们。 argparse 教程提供 a good definition of a positional argument :

[A positional argument is] named so because the program should know what to do with the value, solely based on where it appears on the command line. This concept is more relevant to a command like cp, whose most basic usage is cp SRC DEST. The first position is what you want copied, and the second position is where you want it copied to.

在这种情况下,当调用 Experimental.py 时,您省略了名为 role 的位置参数,该参数是在程序末尾附近定义的:

parser.add_argument('role', choices=choices, help='which role to play')

(因为第一个参数 ('role') 不是以 - 开头(如 -p),add_argument registers it as a positional argument .)

The choices keyword argument to add_argument requires an iterable containing the possible values to supply ,因此程序的作者传递了 choices,字典定义为:

choices = {'client': client, 'server': server}

这实际上非常聪明;迭代字典会产生键,因此 argparse 允许 clientserver 作为 role 的值。因此,要修复此错误,您应该将 clientserver 作为参数传递给程序,从而为 role 提供一个值。更好的是,您可以查看 argparse tutorial 。 (我讨厌告诉人们阅读手册,但我认为这可能会有所帮助)

tl;dr: 而不是:

$ python3 Experimental.py

您应该尝试以下方法之一:

$ python3 Experimental.py server
$ python3 Experimental.py client

关于python - 试图做简单的 UDP 服务器客户端时 Argparse 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33836246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com