【发布时间】:2017-06-16 01:10:52
【问题描述】:
过程是我向服务器发送票证,服务器将响应我需要的信息。例如,
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 作品:
<?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 中做同样的事情(使用或不使用请求都可以接受)。
binary_data = "����5UU��ǚ����������h�V����������������������� ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������"
【问题讨论】:
-
Pyghon 是某种库吗?
-
PHP不会将Ticket作为 POST 数据发送,而是在 url 中发送 - 请参阅"${BASE_SERVER_URL}?" . $postFields- 所以它使用 urlhttps://ssl.XXX.com/sql_v5i.php?A=1&B=2。你可以在requests中使用params=Ticket来做同样的事情。然后你可以data=发送二进制数据。
标签: php python post server python-requests