A
A
Andrew2017-01-10 22:54:44
Python
Andrew, 2017-01-10 22:54:44

How to pass cookies to a website using python3?

For normal work on the site through python in my personal accounts, I want to transfer cookies to the site.
I get it like this:

import http.cookiejar, urllib.request
import http.client, urllib.parse, requests, json
import time
import re, os
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
params = urllib.parse.urlencode({'identity' : 'LOGIN',
                       'password' : 'PAROL4ik' 
                                 })
r = opener.open('http://site.../auth/login', params.encode('utf-8'))
for i in cj:
    cok=i.value
print(cok)

print(cok) = cookie value.
How to send them back? I'm trying through the requests module, because I plan to upload files.
something like code (taken from docs.python-requests.org/en/master/user/quickstart...
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')

>>> r = requests.get(url, cookies=cookies)

but I'm trying to simply send at least cookies, without any data - it redirects to the login.
I add data by login/password and cookies - it's still a redirect.
Disabling the redirect allow_redirects=False - empty, but the idea wants to redirect :)
Tell me how to pass cookies?
ps python3, preferably, very desirable on requests, because I want to send files with one request.
Thanks :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-01-10
@sim3x

all OK

In [6]: r.text
Out[6]: '{\n  "cookies": {\n    "cookies_are": "working"\n  }\n}\n'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question