У меня есть код в PHP, как это сделать в Python CURL - PullRequest
0 голосов
/ 29 мая 2019

У меня есть некоторая проблема, у одного человека есть сервер Ubuntu без php, и этот человек не хочет устанавливать php на этот сервер, мне нужно загрузить файл, используя для них cURL или python, но я знаю только, как это сделать в php , У меня нет каких-либо основных шагов с Python :(. Кто-то может помочь мне с этим основанием мой код с php. Я пробую что-то вроде этого

Python

with open('plik.csv', 'rb') as csv_file:
    r = requests.post(https://somelink/companies, data=csv_file)

Curl

curl --header "Content-Type: text/csv" --request POST --data-binary "@test.csv" https://somelink/companies

Что я делаю не так

$url = 'https://somelink/companies';

$params = array(
    'key' => '8e7bdd23qa1d06b493152ea37cd4ah39',
    'csv' => file_get_contents('test.csv'),
); 

$query = http_build_query($params);

$ctx = stream_context_create(array(
            'http' => array(
                'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($query)."\r\n".
                    "User-Agent: uploader_pmg_companies\r\n",
                'method'  => 'POST',
                'content' => $query,
            ),
        ));

$result = file_get_contents($url, $flags = null, $ctx);

1 Ответ

0 голосов
/ 29 мая 2019
Ok I wrote in python some like this

запросы на импорт

url = "https://somelink/companies"
files = {'file': open('plik.csv', 'rb')}
payload = {'key': '8e7bdd23qa1d06b493152ea37cd4ah39', 'csv': files}
headers = {'Content-Type: application/x-www-form-urlencoded', 'User-Agent: uploader_pmg_companies'}
r = requests.post(url, params=payload, files=files)
print r.url
print (r.text)
And server answer {"file_csv_content":"file...","status":success"}

So this works?
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...