TAGS :Viewed: 11 - Published at: a few seconds ago

[ Curl code to python convert ]

How will look the code in python with requests, if curl code like:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'id='.$id);
$result = curl_exec($ch);

Because this one does not works for me

  id="j4156f9150ece727e38bbf982634ee"  
  cookie = {'id': id}
  content = requests.post(url, cookies=cookie)
  print(id)
  print(cookie)
  print(content.url)
  print(content.cookies)

result is:

['j4156f9150ece727e38bbf982634ee']
{'id': 'j4156f9150ece727e38bbf982634ee'}
http://example.com
<<class 'requests.cookies.RequestsCookieJar'>[]>

WHAT IT MEAN?

Answer 1


>>> import requests
>>> id="j4156f9150ece727e38bbf982634ee"  
>>> cookie = {'id': id}
>>> content = requests.post('http://www.google.com',cookies=cookie)
>>> print id
j4156f9150ece727e38bbf982634ee
>>> print cookie
{'id': 'j4156f9150ece727e38bbf982634ee'}
>>> print content.url
http://www.google.com/
>>> print content.cookies
<<class 'requests.cookies.RequestsCookieJar'>[<Cookie id=j4156f9150ece727e38bbf982634ee for />]>
>>> print content.cookies.get('id')
j4156f9150ece727e38bbf982634ee