curl-and-python

having a hard time converting PHP to Python

From: Arye - GMail <aryeh_at_bigfoot.com>
Date: Wed, 14 Nov 2007 18:41:53 +0100

Hello all,
I am new to the list so sorry for the stupid questions...

I have no success in converting this PHP code to Python:

*************************begin PHP
<?PHP
      echo "<pre>" . $_SERVER['HTTP_USER_AGENT']. "</pre>\n";

      $oCurl = curl_init();

      $sSerial = 'xxxxxxxxxxxxxxxxxxxxx';
      $sHttpLogin = 'xxxxxxxxxxxxxxx';
      $sHttpPwd = 'xxxxxxxxxxxxxxxxxxxxxx';

      echo "<pre>sSerial:" . $sSerial. "</pre>\n";
      echo "<pre>sHttpLogin:" . $sHttpLogin. "</pre>\n";
      echo "<pre>sHttpPwd:" . $sHttpPwd. "</pre>\n";

      $sCalledurl =
'https://someservice-ws.somesite.com/index.php?d=webServices_Server&c=ServerRest';

      $aParameters = array
                     ( 'rm' => 'importFileContr'
                      , 'rf' => 'importProduct'
                      , 'sl' => $sSerial
                      , 'FILENAME' =>
                     '@/webserver/htdocs/pp_files/pp_product2.csv' );
      // Do not forget '@' before the file to be called

      //Option: Url to call
      curl_setopt( $oCurl, CURLOPT_URL, $sCalledurl );

      //Option: Do not display the result but store it in a variable
      curl_setopt( $oCurl, CURLOPT_RETURNTRANSFER, 1);

      //Option: Set HTTPS Login And password
      curl_setopt( $oCurl, CURLOPT_USERPWD, $sHttpLogin.':'.$sHttpPwd );
      curl_setopt( $oCurl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
      curl_setopt( $oCurl, CURLOPT_UNRESTRICTED_AUTH, true);

      curl_setopt( $oCurl, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt( $oCurl, CURLOPT_SSL_VERIFYPEER, false);

      //Option: Active HTTP Post
      curl_setopt( $oCurl, CURLOPT_POST, 1);
      curl_setopt( $oCurl, CURLOPT_HTTPHEADER, array("Expect:") );

      //Option: Set HTTP Parameters
      curl_setopt( $oCurl, CURLOPT_POSTFIELDS, $aParameters );

      //Launch Curl Request
      $sCurlResponse = curl_exec( $oCurl );

      echo "<pre>sCurlResponse\n" . $sCurlResponse . "end</pre>\n";

      exit();

?>
*************************end PHP

This is my Python translation. The call does not go through. Any
thoughts you might have on this would be greatly appreciated !

*************************begin Python
import StringIO
import pycurl
import urllib

merchant_serial = "xxxxxxxxxxxxxxxxxxxxxx"
merchant_login = 'xxxxxxxxxx'
merchant_password = 'xxxxxxxxxx'

url =
'https://someservice-ws.somesite.com/index.php?d=webServices_Server&c=ServerRest'

contents = StringIO.StringIO()

mycurl = pycurl.Curl()

Parameters = {'rm':'importFileContr',
              'rf':'importProduct',
              'sl':merchant_serial,
              'FILENAME':'c:/webserver/htdocs/pp_files/pp_product.csv'}
# Do not forget '@' before the file to be called

dataupdate=urllib.urlencode(Parameters)

# Option: Url to call
mycurl.setopt(mycurl.URL, url)
#Option: Do not display the result but store it in a variable
#mycurl.setopt(mycurl.RETURNTRANSFER, 1)

mycurl.setopt(mycurl.WRITEFUNCTION, contents.write)

#Option: Set HTTPS Login And password
mycurl.setopt(mycurl.USERPWD, merchant_login+':'+merchant_password)
#mycurl.setopt(mycurl.HTTPAUTH, mycurl.CURLAUTH_ANY)
mycurl.setopt(mycurl.UNRESTRICTED_AUTH, True)

mycurl.setopt(mycurl.SSL_VERIFYHOST, 0)
mycurl.setopt(mycurl.SSL_VERIFYPEER, False)

#Option: Active HTTP Post
mycurl.setopt( mycurl.POST, 1)
mycurl.setopt( mycurl.HTTPHEADER, ["Expect:"] )

#Option: Set HTTP Parameters
mycurl.setopt( mycurl.POSTFIELDS, dataupdate )

#Launch Curl Request
mycurl.perform()

print "contents.getvalue():"
print contents.getvalue()
print "mycurl.getinfo(pycurl.HTTP_CODE):"
print mycurl.getinfo(pycurl.HTTP_CODE)
print "mycurl.getinfo(pycurl.EFFECTIVE_URL):"
print mycurl.getinfo(pycurl.EFFECTIVE_URL)

mycurl.close()
*************************end Python
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2007-11-14