无法在芹菜django中使用'#'作为代理网址

时间:2018-01-01 15:21:51

标签: django url celery broker

app = Celery('myapp',
             broker='amqp://user:pass#1@localhost:5672//',
             backend='rpc://',
             include=['myapp.tasks'])

我收到此错误

  

ValueError:基数为10的int()的无效文字:'pass'

这段代码不起作用,我是python的新手,而Django是否有转义序列?

我试过你“”,“”“,”#“,”##“和”#“,希望它能逃脱它,但它没有。

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

我知道#是一个问题,因为很明显,如果我从密码中删除该字符就可以完美运行

1 个答案:

答案 0 :(得分:2)

uri的rabbitmq documentation是指RFC3986。他们有一个关于保留字符的部分,而#就是其中之一。

  

如果URI组件的数据与保留的数据冲突     字符作为分隔符的目的,那么冲突的数据必须是     在URI形成之前进行百分比编码。

根据规范,您可以将#替换为%23 - 或者只使用不包含保留字符的密码。

<强>更新

你是对的,但也遵循相同的RFC。 Celery (or kombu) uses urlliburllib tells they want to match the RFC也是。

它在this line崩溃,你的密码被解释为一个端口。似乎密码里面的#强制库将密码解释为端口(由于它不能转换为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密码中转义内容的来源。但说实话 - 这听起来很受欢迎。转义密码中的字符?我建议只选择一个没有#的密码,因为这会混淆pythons URL解析器,也很可能是其他实现。