A
A
Andrew2014-05-10 15:25:45
PHP
Andrew, 2014-05-10 15:25:45

Analogue of php fopen(url with context) in python?

Good afternoon! I have the following php code, please help me translate it into python:

$url = "https://test.ctpe.net/frontend/GenerateToken";
$data = "AMOUNT=4.00&MODE=INTEGRATOR_TEST";
$params = array('http' => array(
       'method' => 'POST',
       'content' => $data
     ));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
$response = @stream_get_contents($fp);

I tried to use requests, but either I'm missing something, or something else, but the data that I get is incorrect, unlike the PHP version.
payload = {
    "TRANSACTION.MODE": "INTEGRATOR_TEST",
    "PRESENTATION.AMOUNT": "4.00"
}
url = "https://test.ctpe.net/frontend/GenerateToken"
post_data = urllib.urlencode(payload)
req = requests.post(url, data=post_data)
response = req.text

I thought how to implement with pom. urllib, but I don't know what parameters and how to use.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leo, 2014-05-10
@aphex

import requests
req = requests.post("https://test.ctpe.net/frontend/GenerateToken", data={"TRANSACTION.MODE": "INTEGRATOR_TEST", "PRESENTATION.AMOUNT": "4.00"}).text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question