简体   繁体   中英

Trouble using pycurl with cookies

I try to get data from a website with pycurl. I made a similar script earlier in php(also using curl) and it worked but in python I get empty response. Similar problem was with php - the site takes post data, initializes a session and redirects to result page, but if no cookies are allowed it returns an empty response instead of redirect. I have no access to code on this site. Using COOKIEFILE and COOKIEJAR options solved the problem. This is the php code:

<?php
$anul = 2009;
$idnp = "2000000000000";
$seria_diploma = "AB000000000";
$url = "http://acte.edu.md/handler.php";
$cookie_file = tempnam("tmp", "cookie");
$curl_session = curl_init($url);
curl_setopt($curl_session, CURLOPT_POST, 1);
$post_fields = "an=$anul&idnp=$idnp&=$seria_diploma&Submit=OK"; 
curl_setopt($curl_session, CURLOPT_POSTFIELDS,$post_fields);
curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_session, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl_session, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl_session);
curl_close($curl_session);
echo $result;

Using the same options in python I get empty response with HTTP code 200 instead of 302. Here is the code i use:

  url = "http://acte.edu.md/handler.php"
  post_data = dict({
            "an":2009,
            "idnp":"2000000000000",
            "a":"AB000000000",
            "Submit":"OK"
  })
  buf = StringIO.StringIO()
  c = pycurl.Curl()

  c.setopt(pycurl.URL,url)
  c.setopt(pycurl.WRITEFUNCTION, buf.write)
  #c.setopt(pycurl.CONNECTTIMEOUT,10000)
  cookiefile = os.tempnam(_APP_ROOT_PATH+"temp_files","cookie")
  c.setopt(pycurl.COOKIEFILE, cookiefile)
  c.setopt(pycurl.COOKIEJAR, cookiefile)
  c.setopt(pycurl.FOLLOWLOCATION, 1)
  #c.setopt(pycurl.MAXREDIRS, 5)
  c.setopt(pycurl.POST, 1)
  c.setopt(pycurl.POSTFIELDS, urllib.urlencode(post_data))
  c.setopt(pycurl.VERBOSE, True)
  c.perform()

  response += "ERROR:" + c.errstr()
  response += c.getinfo(pycurl.HTTP_CODE).__str__()+ c.getinfo(pycurl.EFFECTIVE_URL)
  c.close()     

Please tell me if your have any suggestions...

The problem was in invalid POST parameters. If they don't pass validation page returns empty response.

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