app = Celery('myapp',
broker='amqp://user:pass#1@localhost:5672//',
backend='rpc://',
include=['myapp.tasks'])
我收到此错误
ValueError:int() 的文字无效,底数为 10:"pass">
这段代码不起作用,我是 python 和 Django 的新手,它有转义序列吗?
Iv尝试了u","r","#","##"和"#",希望它能逃脱它,但它没有。
Traceback (most recent call last):
File "C:Program Files (x86)Microsoft Visual StudioSharedPython36_64librunpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:Program Files (x86)Microsoft Visual StudioSharedPython36_64librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:UsersusersourcereposBtcApiBtcApibtcapienvScriptscelery.exe__main__.py", line 9, in <module>
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelery__main__.py", line 14, in main
_main()
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybincelery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybincelery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybinbase.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybincelery.py", line 480, in handle_argv
return self.execute(command, argv)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybincelery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybinworker.py", line 221, in run_from_argv
return self(*args, **options)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybinbase.py", line 244, in __call__
ret = self.run(*args, **kwargs)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagescelerybinworker.py", line 255, in run
**kwargs)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagesceleryworkerworker.py", line 99, in __init__
self.setup_instance(**self.prepare_args(**kwargs))
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagesceleryworkerworker.py", line 120, in setup_instance
self._conninfo = self.app.connection_for_read()
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagesceleryappbase.py", line 752, in connection_for_read
return self._connection(url or self.conf.broker_read_url, **kwargs)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packagesceleryappbase.py", line 828, in _connection
'broker_connection_timeout', connect_timeout
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packageskombuconnection.py", line 181, in __init__
url_params = parse_url(hostname)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packageskombuutilsurl.py", line 34, in parse_url
scheme, host, port, user, password, path, query = _parse_url(url)
File "c:usersusersourcereposbtcapibtcapibtcapienvlibsite-packageskombuutilsurl.py", line 52, in url_to_parts
parts.port,
File "C:Program Files (x86)Microsoft Visual StudioSharedPython36_64liburllibparse.py", line 167, in port
port = int(port, 10)
ValueError: invalid literal for int() with base 10: 'pass
我知道#是一个问题,因为显然如果我从密码中删除该字符,它可以完美运行
uri 的 rabbitmq 文档指的是RFC3986。他们有一个关于保留字符的部分,#
就是其中之一。
如果 URI 组件的数据与保留的数据冲突 字符作为分隔符的目的,则冲突数据必须是 在形成 URI 之前进行百分比编码。
根据规范,您可以将#
替换为%23
- 或者只使用不包含保留字符的密码。
更新
你是对的,但那里也遵循相同的 RFC。Celery(或kombu)使用urllib,urllib告诉他们也想匹配RFC。
它在此行中崩溃,您的密码被解释为端口。似乎密码内部的#
强制库将密码解释为端口(失败,因为它不可转换为 int)。请注意,域和端口由与用户名和密码相同的字符:
分隔。
下面说明发生了什么。请注意,#
之后的所有内容都会被解释为网址的片段。
>>> from urllib.parse import urlparse
>>> url = 'amqp://user:pass#1@localhost:5672//'
>>> urlparse(url)
ParseResult(scheme='amqp', netloc='user:pass', path='', params='', query='', fragment='1@localhost:5672//')
看看我们移除#
后会发生什么
>>> url = 'amqp://user:pass%231@localhost:5672//'
>>> urlparse(url)
ParseResult(scheme='amqp', netloc='user:pass%231@localhost:5672', path='//', params='', query='', fragment='')
>>> urlparse(url).port
5672
>>> urlparse(url).password
'pass%231'
网址可以正确解析 - 但我想密码现在错了。可悲的是,我找不到任何描述如何在 URI 密码中转义某些内容的来源。但说实话 - 这听起来很奇怪。密码中的转义字符?我建议只选择一个密码而不#
因为这会混淆python的URL解析器,很可能还会混淆其他实现。