简体   繁体   English

如何建立一个包含来自Python用户输入的用户名和密码的URL?

[英]How do you build a URL that contains a username and password from user input in Python?

I see you can parse usernames and passwords with: 我看到您可以使用以下方式解析用户名和密码:

from urlparse import urlparse
r = urlparse('http://myuser:mypass@example.com')
print r.username
# => 'myuser'

How can I go the other way? 我该怎么走呢? I can't use urlunparse because I can't do this: 我不能使用urlunparse因为我不能这样做:

r.username = request.args['username']
# => AttributeError: can't set attribute

I'm interested because the username contains characters that need escaped (namely: @ , / ). 我很感兴趣,因为用户名包含需要转义的字符(即: @/ )。

Edit: 编辑:

This string is coming from user input, so I won't know ahead of time what it is. 该字符串来自用户输入,因此我不会提前知道它是什么。 It's a security risk to maintain your own list of escape characters, so string concatenation and custom character escaping with replace won't help here. 维护您自己的转义字符列表会带来安全隐患,因此,字符串连接和使用replace自定义字符转义将无济于事。

Turns out the username part of basic auth URLs use the same escape characters that query parameters do. 事实证明,基本身份验证URL的用户名部分使用与查询参数相同的转义字符

So the answer is to use urllib.quote(username, safe='') then concatenate. 因此答案是使用urllib.quote(username, safe='')然后进行连接。 ( You'll need the safe parameter or / won't be escapted.) 你需要safe参数或/不会escapted。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何比较从 a.txt 读取的两个列表(用户名和密码)与用户输入 - Python: how do I compare two lists (username & password) read from a .txt to a user input Python - 如何存储输入的用户名/密码 - Python - How to store a username/password from input 如何在python中使用网址执行mysql包含的内容? - How do you perform mysql contains with a url in python? 如何在python中使用ADAL使用Azure区块链工作台的用户名和密码获取身份验证承载? - How do you get authentication bearer using a username and password for the Azure Blockchain Workbench using ADAL in python? Python 处理 URL 的用户名和密码 - Python handling username and password for URL 如何在 python 中输入固定的用户名和密码而不是要求输入字段? - How do I enter a fixed username and password rather than requiring an input field in python? 如何从auth_user django python中选择给定用户名和哈希密码的用户 - how to select user from auth_user django python for given username and hashed password 如何从 Python 中的 root 用户获取普通用户名? - How do I get the normal username from the root user in Python? 如何使用Python中的数组从用户输入字符列表 - How do you input a list of characters from the user using arrays in Python 如何在python中从多行用户输入中打印最大和最小数字? - How do you print the maximum and minimum numbers from multiple lines of user input in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM