python urlparse库将application/x-www-form-urlencoded转换为字典

data of type application/x-www-form-urlencoded转换为字典形式


python官网有解释:https://docs.python.org/2/library/urlparse.html

urlparse. parse_qs ( qs [keep_blank_values [strict_parsing ] ] )

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.

The optional argument keep_blank_values is a flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included.

The optional argument strict_parsing is a flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception.

Use the urllib.urlencode() function to convert such dictionaries into query strings.


参考:http://stackoverflow.com/questions/6877497/converting-x-www-form-urlencoded-query-string-to-a-dict

<think>好的,用户遇到了Invalid URI错误,具体URI是'%27http://43.134.134.152:20050/v3/live/room/51/user/1407374883553400/notice/system%27'。我需要分析可能的原因,并提供解决方案。 首先,查看URI的结构。注意到URI开头有%27,这是URL编码的单引号。可能用户或程序错误地将单引号包含在URI中,导致解析失败。正确的URI不应包含未义的特殊字符,尤其是作为分隔符的单引号。 其次,检查是否有编码错误。%27代表单引号,可能在构造URI时,单引号被错误地编码进去。例如,如果URI被包裹在单引号中,并且又被URL编码,就会产生这样的问题。需要确认是否在拼接URI时多加了单引号,或者编码过程中出现了重复编码的情况。 另外,检查服务端是否接受包含特殊字符的URI。某些服务器可能对特殊字符有严格限制,需要确保URI符合服务端的预期格式。例如,路径部分中的参数可能需要进一步编码,或者服务端路由配置不正确,无法处理该URI结构。 参考引用中的信息,用户之前遇到了SSL连接问题,但当前问题不同。不过,引用[3]提到补丁处理,可能与服务器配置有关,但这里更可能是客户端构造URI的问题。需要确保URI的各个部分正确义,没有多余字符。 可能的解决方案包括:移除多余的单引号,检查编码是否正确,验证服务器配置,以及使用工具测试URI有效性。同时,提供相关帮助问题,帮助用户进一步排查。</think>### Invalid URI错误分析与解决方法 出现`Invalid URI`错误通常与URI格式不规范或非法字符有关。针对您提供的URI: ``` %27http://43.134.134.152:20050/v3/live/room/51/user/1407374883553400/notice/system%27 ``` #### 原因分析: 1. **非法字符编码**: - URI开头的`%27`是URL编码后的单引号字符`'`,结尾同样存在`%27`。单引号在URI中属于保留字符,不能直接出现在路径部分,需检查是否错误添加了引号包裹。 - 示例错误:将URI写成`'http://.../system'`(带单引号),导致编码后出现`%27`。 2. **双重编码问题**: - 可能对已编码的URI进行了二次编码,例如路径参数未正确义。例如,路径中的数字`1407374883553400`若包含特殊字符需单次编码。 3. **服务端路由不匹配**: - 服务端可能未定义`/v3/live/room/51/user/1407374883553400/notice/system`路由,或对URI长度、格式有限制[^3]。 #### 解决步骤: 1. **移除多余引号**: ```python # 原始URI含多余的单引号编码 bad_uri = "%27http://43.134.134.152:20050/.../system%27" # 修正后 fixed_uri = bad_uri.strip("%27") # 移除首尾的%27 ``` 修正后的URI应为: ``` http://43.134.134.152:20050/v3/live/room/51/user/1407374883553400/notice/system ``` 2. **检查编码一致性**: - 使用标准验证URI合法性: ```python from urllib.parse import urlparse try: result = urlparse("http://43.134.134.152:20050/.../system") assert all([result.scheme, result.netloc]) # 必须包含协议和域名 except ValueError: print("Invalid URI") ``` 3. **服务端验证**: - 通过`curl`测试服务端可达性: ```bash curl -v "http://43.134.134.152:20050/v3/live/room/51/user/1407374883553400/notice/system" ``` - 若返回`404`,需检查服务端路由配置;若返回`400`,需检查URI参数格式。 4. **网络调试工具**: - 使用Wireshark或浏览器开发者工具查看实际发送的请求,确认URI是否被篡改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值