Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and curl question

I will be transmitting purchase info (like CC) to a bank gateway and retrieve the result by using Django thus via Python.

What would be the efficient and secure way of doing this?

I have read a documentation of this gateway for php, they seem to use this method:

$xml= Some xml holding data of a purchase.
$curl = `/usr/bin/curl -s -d 'DATA=$xml' "https://url of the virtual bank POS"`;
$data=explode("\n",$curl); //return value is also an xml, seems like they are splitting by each `\n`

and using the $data, they process if the payment is accepted, rejected etc..

I want to achieve this under python language, for this I have done some searching and seems like there is a python curl application named pycurl yet I have no experience using curl and do not know if this is library is suitable for this task. Please keep in mind that as this transfer requires security, I will be using SSL.

Any suggestion will be appreciated.

like image 201
Hellnar Avatar asked Dec 12 '22 23:12

Hellnar


2 Answers

Use of the standard library urllib2 module should be enough:

import urllib
import urllib2

request_data = urllib.urlencode({"DATA": xml})
response = urllib2.urlopen("https://url of the virtual bank POS", request_data)

response_data = response.read()
data = response_data.split('\n')

I assume that xml variable holds data to be sent.

like image 112
Łukasz Avatar answered Dec 28 '22 16:12

Łukasz


Citing pycurl.sourceforge.net:

To sum up, PycURL is very fast (esp. for multiple concurrent operations) and very feature complete, but has a somewhat complex interface. If you need something simpler or prefer a pure Python module you might want to check out urllib2 and urlgrabber. There is also a good comparison of the various libraries.

Both curl and urllib2 can work with https so it's up to you.

like image 45
Krab Avatar answered Dec 28 '22 16:12

Krab