简体   繁体   中英

Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,))

I don't understand why the connection aborted, I have included headers and cookies in my request. Any input would be highly appreciated.

import requests


url = 'https://fp.trafikverket.se/boka/#/search/SPHhISIPAfhPP/5/0/0/0'
s = requests.session()
res = s.get(url)
cookies = dict(res.cookies)
headers = dict(res.headers)
headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
r = s.post(url, 
    cookies=cookies, headers=headers).text

>>> requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(54, 'ECONNRESET')",))

Try the below code, it works.

import requests
import json

url = 'https://fp.trafikverket.se/boka/#/search/SPHhISIPAfhPP/5/0/0/0'
s = requests.session()
res = s.get(url)
cookies = dict(res.cookies)
headers = dict(res.headers)
headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
data = {}
r = s.post(url, data = json.dumps(data), cookies = cookies, headers = headers).text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM