这个过程是我向服务器发送一张票,服务器会回复我需要的信息。例如,
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 “
答案 0 :(得分:0)
PHP
示例不会将Ticket
作为POST
数据发送,而是作为GET
数据发送 - 这意味着在网址
https://ssl.XXX.com/sql_v5i.php?A=1&B=2
您可以使用params=Ticket
在requests
中执行相同操作。然后,您可以使用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"
}
您还可以使用本地代理服务器(即Charles)来比较PHP请求和Python请求。
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)