Apr-24-2020, 03:00 AM
So sorry that I can't describe accurately in the title because I don't really understand this issue.
I have a simple Py file which simply post some data to a website.It worked very well on my Windows 10 and windows 7, but got strange error on another windows 7. Part of the codes:
The first error is
If I use encoding = "utf-8" instead of encoding = "utf-8-sig", the error is
The content of 1.txt here,it's a cookie file.
I have a simple Py file which simply post some data to a website.It worked very well on my Windows 10 and windows 7, but got strange error on another windows 7. Part of the codes:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
url = 'https://someweb.com/submit'
headers={
"Host": "something.com",
"Connection": "keep-alive",
"Content-Length": "100",
}
def format(Cookie):
cookies = {}
for line in Cookie.split(";"):
if line.find("=") != -1:
name, value = line.strip().split("=")
cookies[name] = value
return cookies
fp = open ("1.txt","r")
Cookie = fp.read()
fp.close()
CookieNew = format(Cookie)
DataNew = '{"vId":"000104","serviceId":"1002"}'
requests.post(url, data=DataNew, headers=headers, cookies=CookieNew)The format(cookie) function changes the format to JSON I guess because the website only accept this format.(Googled)The first error is
Error:UnicodeEncodeError: 'latin-1' codec can't encode characters in position xx-xx:
ordinal not in range(256)Then I googled and changed the fp = open ("1.txt","r") to fp = open ("1.txt","r",encoding = "utf-8-sig")Now I got this error from website: {"timestamp":"2020-04-23 16:47:15","status":500,"error":"Internal
Server Error","exception":"org.springframework.http.converter.HttpMessageNotRead
ableException","message":"Could not read document: Unexpected character ('i' (co
de 105)): was expecting comma to separate OBJECT entries\n at [Source: java.io.P
ushbackInputStream@6bc0aae7; line: 1, column: 105]; nested exception is com.fast
erxml.jackson.core.JsonParseException: Unexpected character ('i' (code 105)): wa
s expecting comma to separate OBJECT entries\n at [Source: java.io.PushbackInput
Stream@6bc0aae7; line: 1, column: 105]","path":"/dir/cou/submit"}I'm confused that this is the same py file. What makes this error and how do I google this because it looks like java responses.If I use encoding = "utf-8" instead of encoding = "utf-8-sig", the error is
Error:UnicodeEncodeError: 'latin-1' codec can't encode character '\ufeff' in position
xx: ordinal not in range(256)The OS of windows 10 and the widnows 7 with py error are Asia version,the other windows7 without error is English version.The content of 1.txt here,it's a cookie file.
Output:Hm_lvt_4a693f348daf8481dd541c844122c053=1585791910; JSESSIONID=D524881BB65001A53B445AA4EDC14FC7; Hm_lpvt_4a693f348daf8481dd541c844122c053=1587654145; Hm_lvt_bc864c0a0574a7cabe6b36d53206fb69=1583560881; Thanks for help and sorry for the chaotic description.
