My environment: Windows platform Command line encoding gbk,python2.7.6. Need to use Python to submit a Chinese verification code, the destination address of the page encoded as Utf-8, the current self-simulation server. The HTTP request uses the Python requests module, but the returned results are often not normal and the code is as follows:
Client:
# python代码code = '中文验证'# 这里面的code应该是unicode字符串了post_data = { 'name':'jack', 'password':'123456', 'code':code # u'\u4E2D\u6587\u9A8C\u8BC1'}res = requests.post('http://www.test.com',data=post_data)# 使结果集正确显示中文res = res.contentunicode(res,"utf-8")if '验证码有误' in res: print 'authcode error,retry...'else: print 'success'
#这是服务端的PHP代码if ($_POST['code'] == '中文验证'){ $result = $db->insert($_POST);} else { echo '验证码有误'; $_POST['code'] = $_POST['code'].'e'; $result = $db->insert($_POST);}
Then the strange thing appeared, the Database Code field inserted some "Chinese authentication" and some "Chinese authentication e", what is this situation? If there is a problem with coding, how can you sometimes judge the error by judging it correctly?
Supplemental statement:
No garbled problem, inserting the database is normal, in fact, in the definition of post_data dic, Python itself has the Chinese Unicode, but why this string of Unicode submitted to the server and "Chinese authentication" comparison, sometimes error, sometimes correct?
Reply content:
My environment: Windows platform Command line encoding gbk,python2.7.6. Need to use Python to submit a Chinese verification code, the destination address of the page encoded as Utf-8, the current self-simulation server. The HTTP request uses the Python requests module, but the returned results are often not normal and the code is as follows:
Client:
# python代码code = '中文验证'# 这里面的code应该是unicode字符串了post_data = { 'name':'jack', 'password':'123456', 'code':code # u'\u4E2D\u6587\u9A8C\u8BC1'}res = requests.post('http://www.test.com',data=post_data)# 使结果集正确显示中文res = res.contentunicode(res,"utf-8")if '验证码有误' in res: print 'authcode error,retry...'else: print 'success'
#这是服务端的PHP代码if ($_POST['code'] == '中文验证'){ $result = $db->insert($_POST);} else { echo '验证码有误'; $_POST['code'] = $_POST['code'].'e'; $result = $db->insert($_POST);}
Then the strange thing appeared, the Database Code field inserted some "Chinese authentication" and some "Chinese authentication e", what is this situation? If there is a problem with coding, how can you sometimes judge the error by judging it correctly?
Supplemental statement:
No garbled problem, inserting the database is normal, in fact, in the definition of post_data dic, Python itself has the Chinese Unicode, but why this string of Unicode submitted to the server and "Chinese authentication" comparison, sometimes error, sometimes correct?
Here are a few outrageous, for reference only
- Check py2 and PHP code with np++ UTF8 no BOM
- Py code added coding encoding Declaration
- It is best to use HTTP code to indicate errors when the server returns, instead of using text
My suggestion is that if you want to compare Chinese strings, they will be converted to Unicode and then compared.