Use existing authenticated session from browser to perform https request on python

后端 未结 3 1884
清歌不尽
清歌不尽 2021-02-15 10:55

Is it possible to take an existing logged in session (say in Chrome) and pipe that session to a python script to perform an https request?

To be clear on what I want to

相关标签:
3条回答
  • 2021-02-15 11:35

    This should be possible if you reuse your browser's cookies and user agent. As far as I can see, any such solution would be browser-specific though: I have come across a script that uses SQLite to extract Chrome cookies and use them to make HTTP requests with the Requests library.

    The script's chrome_cookies method returns a dictionary containing cookies. If you use the Requests library, you can pass the dictionary as a keyword argument when making requests:

    import requests
    import pyCookieCheat
    
    url = 'http://www.example.com'
    
    s = requests.Session()
    cookies = pyCookieCheat.chrome_cookies(url)
    s.get(url, cookies = cookies)
    
    0 讨论(0)
  • 2021-02-15 11:40

    Why don't you parse out the generated CAPTCHA, display the image and manually input the solution? It may be an easier approach to your problem than actually hijacking a session. Plus, it would result in a more portable and stable script (probably).

    0 讨论(0)
  • 2021-02-15 11:41

    This might help

    jar = requests.cookies.RequestsCookieJar([
    {
        "domain": ".stackoverflow.com",
        "expirationDate": "1427212131.77312",
        "hostOnly": "false",
        "httpOnly": "true",
        "name": "usr",
        "path": "/",
        "secure": "false",
        "session": "false",
        "storeId": "0",
        "value": "SOMEVALUE",
        "id": "5"
    }]
    requests.get(url, headers=headers, cookies=jar)
    

    @Stupid.Fat.Cat Let me know what worked for you

    0 讨论(0)
提交回复
热议问题