2017-01-30 3 views
0

プロセスはチケットをサーバーに送信し、サーバーは必要な情報で応答します。例えば、Pythonを使用してサーバーにキーと値のペアとバイナリ情報を一緒に送信する方法

import requests 
URL = "https://ssl.XXX.com/sql_v5i.php" 
Ticket = {"A":"1","B":"2"} # a dictionary 
ticket_post = requests.post(URL, data=Ticket) 
print ticket__post.text 

しかし、どのように一度に全部(データのみなしキー付き)"A":"1", "B":"2"とバイナリデータでチケットを送信するには?

<?php 

$BASE_SERVER_URL="https://ssl.XXX.com/sql_v5i.php" 
$TICKET = array(
      'fn' => 'ticket', 
      'testid' => '2' 
      ... # And many other key-value pair in the TICKET array 
     );  

class DataStreamer{ 
    private $data; 
    private $pos; 

function __construct($data) { 
    $this->data = $data; 
    $this->pos = 0; 
} 

function stream_function($handle, $fd, $count){ 
$res = substr($this->data, $this->pos, $count); 
$this->pos += strlen($res); 
return $res; 
} 
} 

function sendTicketToServer($data) { 
global $TICKET, $BASE_SERVER_URL; # TICKET is an array stores ticket   
            # information in php 
$ret = array(true); 
$postFields = ""; 
foreach ($TICKET as $name => $val) 
    $postFields = (empty($postFields) ? "" : "${postFields}&") ."${name}=" . urlencode($val); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_URL, "${BASE_SERVER_URL}?" . $postFields); 
             # add key-value pairs 
curl_setopt($ch, CURLOPT_VERBOSE, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 

if (!empty($data)) { # data is the binary string , add binary string 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/octet-stream', 'Content-length: ' . strlen($data))); 
    curl_setopt($ch, CURLOPT_READFUNCTION, array(new DataStreamer($data), "stream_function")); 
    } else { 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/octet-stream', 'Content-length: 0')); 
} 

$resp = curl_exec($ch); 
$code=curl_getinfo($ch, CURLINFO_HTTP_CODE); 
if($resp === FALSE || $code !== 200) { 
    echo "!!!!! Failed to get connection from the server. Code: $code\n"; 
    return array(ERR_NO_SERVER_CONNECTION."000"); 
} 

curl_close($ch); 
return $ret; 

はPythonでこれと同じ操作を行う方法(要求を使用するか、またはその両方許容されない):

は、PHPの作品の一部が存在し、このタスクをcomoleteします。

binary_data = " 5UU ǚ h V "

+0

_Pyghon_はある種のライブラリですか? – AbraCadaver

+0

'PHP'は' Ticket'をPOSTデータとして送りませんが、URLに '$ {BASE_SERVER_URL}? ' 。 $ postFields' - それはurl 'https://ssl.XXX.com/sql_v5i.php?A = 1&B = 2'を使用します。 '要求'に 'params = Ticket'を使って同じことをすることができます。そして、あなたは 'data ='バイナリデータを送ることができます。 – furas

答えて

0

PHPの例では、TicketPOSTなどのデータを送信しませんが、GETデータとして - それは、URL

https://ssl.XXX.com/sql_v5i.php?A=1&B=2 

あなたはrequestsで同じことを行うためにparams=Ticketを使用することができますを意味します。そして、data=またはfiles=を使用してバイナリデータを送信することができます。

私は特別なポータルhttpbin.orgを使用してリクエストをテストしていますが、あなたのポータルはより多くの作業を必要とします。

import requests 

url = "http://httpbin.org/post" 
ticket = {"A":"1","B":"2"} 
binary_data = b'hello world' 

headers = { 
    'Content-type': 'application/octet-stream', 
} 

r = requests.post(url, params=ticket, data=binary_data, headers=headers) 

print('--- requests ---') 
print(r.url) 
print(r.request.headers) 
print(r.request.body) 

print('--- response ---') 
print(r.text) 

結果:

--- requests --- 
http://httpbin.org/post?B=2&A=1 
{'Content-type': 'application/octet-stream', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '11', 'User-Agent': 'python-requests/2.12.1', 'Connection': 'keep-alive', 'Accept': '*/*'} 
b'hello world' 
--- response --- 
{ 
    "args": { 
    "A": "1", 
    "B": "2" 
    }, 
    "data": "hello world", 
    "files": {}, 
    "form": {}, 
    "headers": { 
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "11", 
    "Content-Type": "application/octet-stream", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.1" 
    }, 
    "json": null, 
    "origin": "xxx.xxx.xxx.xxx", 
    "url": "http://httpbin.org/post?B=2&A=1" 
} 

ます。また、Pythonの要求でPHPリクエストを比較するために、ローカルプロキシサーバー(すなわちCharles)を使用することができます。

PHP:

curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 

のPython:

proxy = { 
    'http': '127.0.0.1:8888', 
    'https': '127.0.0.1:8888', 
} 

r = requests.post(url, params=ticket, data=binary_data, 
        headers=headers, proxies=proxy) 

enter image description here

+0

ありがとうございます。それは多くの助けになります。この例では、binary_data = b'hello world 'です。しかし、私の場合、binary_data = " 5UU ǚ h V ... ..... "(コメント欄にすべてを入れるのは時間がかかりすぎます)。コードでは、 "SyntaxError:行X(バイナリ文字列行)のファイルに非ASCII文字 '\ xef'が表示されますが、エンコードは宣言されていません。詳細については、http://python.org/dev/peps/pep-0263/を参照してください。この「バイナリデータ」をサーバに送信する方法を教えてください。 –

関連する問題