无法在 celery django 中使用“#”作为代理 url
Unable to use '#' as broker url in celery django
app = Celery('myapp',
broker='amqp://user:pass#1@localhost:5672//',
backend='rpc://',
include=['myapp.tasks'])
我收到这个错误
ValueError: invalid literal for int() with base 10: 'pass'
此代码无效,我是 python 的新手,Django 是否有转义序列?
我试过 u"", r"" ,'#' , '##' 和 '#' ,希望它能逃脱它,但它没有。
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\user\source\repos\BtcApi\BtcApi\btcapienv\Scripts\celery.exe\__main__.py", line 9, in <module>
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\__main__.py", line 14, in main
_main()
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\base.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 480, in handle_argv
return self.execute(command, argv)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\worker.py", line 221, in run_from_argv
return self(*args, **options)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\base.py", line 244, in __call__
ret = self.run(*args, **kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\worker.py", line 255, in run
**kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\worker\worker.py", line 99, in __init__
self.setup_instance(**self.prepare_args(**kwargs))
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\worker\worker.py", line 120, in setup_instance
self._conninfo = self.app.connection_for_read()
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\app\base.py", line 752, in connection_for_read
return self._connection(url or self.conf.broker_read_url, **kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\app\base.py", line 828, in _connection
'broker_connection_timeout', connect_timeout
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\connection.py", line 181, in __init__
url_params = parse_url(hostname)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\utils\url.py", line 34, in parse_url
scheme, host, port, user, password, path, query = _parse_url(url)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\utils\url.py", line 52, in url_to_parts
parts.port,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\parse.py", line 167, in port
port = int(port, 10)
ValueError: invalid literal for int() with base 10: 'pass
我知道 # 是个问题,因为很明显,如果我从密码中删除该字符,它就可以正常工作
rabbitmq documentation for the uri refers to RFC3986。他们有一个关于保留字符的部分,#
就是其中之一。
If data for a URI component would conflict with a reserved
character's purpose as a delimiter, then the conflicting data must be
percent-encoded before the URI is formed.
根据规范,您可以将 #
替换为 %23
- 或者只使用不包含保留字符的密码。
更新
你是对的,但那里也遵循相同的 RFC。 Celery (or kombu) uses urllib and urllib tells they want to match the RFC,也是。
它在 this line 中崩溃,您的密码在那里被解释为一个端口。似乎密码中的 #
强制库将密码解释为端口(失败,因为它不可转换为 int)。请注意,域和端口由与用户名和密码相同的字符 :
分隔。
下面说明了发生了什么。请注意,#
之后的所有内容都被解释为 url.
的片段
>>> 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'
url 可以正确解析 - 但我猜现在密码是错误的。可悲的是,我找不到任何资源来描述如何在 URI 的密码中转义某些内容。但老实说——这听起来很奇怪。密码中的转义字符?我建议只选择一个没有 #
的密码,因为这会混淆 pythons URL 解析器和很可能的其他实现。
app = Celery('myapp',
broker='amqp://user:pass#1@localhost:5672//',
backend='rpc://',
include=['myapp.tasks'])
我收到这个错误
ValueError: invalid literal for int() with base 10: 'pass'
此代码无效,我是 python 的新手,Django 是否有转义序列?
我试过 u"", r"" ,'#' , '##' 和 '#' ,希望它能逃脱它,但它没有。
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\user\source\repos\BtcApi\BtcApi\btcapienv\Scripts\celery.exe\__main__.py", line 9, in <module>
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\__main__.py", line 14, in main
_main()
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\base.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 480, in handle_argv
return self.execute(command, argv)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\celery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\worker.py", line 221, in run_from_argv
return self(*args, **options)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\base.py", line 244, in __call__
ret = self.run(*args, **kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\bin\worker.py", line 255, in run
**kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\worker\worker.py", line 99, in __init__
self.setup_instance(**self.prepare_args(**kwargs))
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\worker\worker.py", line 120, in setup_instance
self._conninfo = self.app.connection_for_read()
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\app\base.py", line 752, in connection_for_read
return self._connection(url or self.conf.broker_read_url, **kwargs)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\celery\app\base.py", line 828, in _connection
'broker_connection_timeout', connect_timeout
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\connection.py", line 181, in __init__
url_params = parse_url(hostname)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\utils\url.py", line 34, in parse_url
scheme, host, port, user, password, path, query = _parse_url(url)
File "c:\users\user\source\repos\btcapi\btcapi\btcapienv\lib\site-packages\kombu\utils\url.py", line 52, in url_to_parts
parts.port,
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\urllib\parse.py", line 167, in port
port = int(port, 10)
ValueError: invalid literal for int() with base 10: 'pass
我知道 # 是个问题,因为很明显,如果我从密码中删除该字符,它就可以正常工作
rabbitmq documentation for the uri refers to RFC3986。他们有一个关于保留字符的部分,#
就是其中之一。
If data for a URI component would conflict with a reserved character's purpose as a delimiter, then the conflicting data must be percent-encoded before the URI is formed.
根据规范,您可以将 #
替换为 %23
- 或者只使用不包含保留字符的密码。
更新
你是对的,但那里也遵循相同的 RFC。 Celery (or kombu) uses urllib and urllib tells they want to match the RFC,也是。
它在 this line 中崩溃,您的密码在那里被解释为一个端口。似乎密码中的 #
强制库将密码解释为端口(失败,因为它不可转换为 int)。请注意,域和端口由与用户名和密码相同的字符 :
分隔。
下面说明了发生了什么。请注意,#
之后的所有内容都被解释为 url.
>>> 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'
url 可以正确解析 - 但我猜现在密码是错误的。可悲的是,我找不到任何资源来描述如何在 URI 的密码中转义某些内容。但老实说——这听起来很奇怪。密码中的转义字符?我建议只选择一个没有 #
的密码,因为这会混淆 pythons URL 解析器和很可能的其他实现。