python2 和python3 差异导致的问题

python2使用ASCII码作为默认编码,但是python3使用unicode码作为字符串的默认编码。
在python2中,结果如下

str='hello'
str2=u'hello'

print type(str)
print type(str2)

### 结果
#<type 'str'>
#<type 'unicode'>

而在python3中,结果如下

str='hello'
str2=u'hello'
str3=b'hello'

print(type(str))
print(type(str2))
print(type(str3))

###结果
#<class 'str'>
#<class 'str'>
#<class 'bytes'>

差异导致的问题。

  • 2020-6-30
    今天使用了requests发送了请求数据包。其中有一段字符串如下

u"O:24:"GuzzleHttp\Psr7\FnStream":2:{s:33:"\u0000GuzzleHttp\Psr7\FnStream\u0000methods";a:1:{s:5:"close";a:2:{i:0;O:23:"GuzzleHttp\HandlerStack":3:{s:32:"\u0000GuzzleHttp\HandlerStack\u0000handler";s:37:"echo Th1sIsATeStT0ProveAVulnerab1litY";s:30:"\u0000GuzzleHttp\HandlerStack\u0000stack";a:1:{i:0;a:1:{i:0;s:6:"system";}}s:31:"\u0000GuzzleHttp\HandlerStack\u0000cached";b:0;}i:1;s:7:"resolve";}}s:9:"_fn_close";a:2:{i:0;r:4;i:1;s:7:"resolve";}}"
使用python2发送的请求包\u被转换为了\\u导致了出现了错误。

因为线上版本和本地版本环境不同,花费很多时间排查问题。
所以记下来。

总结

排查问题的时候,要把所有的差异都列下来,一一排查其中出现的错误,控制变量法。

相关文章:

  • 2021-07-04
  • 2021-11-02
  • 2022-01-18
  • 2021-10-20
  • 2021-07-13
  • 2021-10-21
  • 2022-12-23
  • 2021-07-15
猜你喜欢
  • 2022-12-23
  • 2021-11-02
  • 2021-12-17
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案