Php 如何使用Python将键值对和二进制信息一起发送到服务器
这个过程是我向服务器发送一个票证,服务器将响应我需要的信息。比如说,Php 如何使用Python将键值对和二进制信息一起发送到服务器,php,python,post,server,python-requests,Php,Python,Post,Server,Python Requests,这个过程是我向服务器发送一个票证,服务器将响应我需要的信息。比如说, 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”和二进制数据(只有数据,没有密钥)的票据
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;
PHP
示例不以POST
数据的形式发送Ticket
,而是以GET
数据的形式发送-它的意思是在url中
https://ssl.XXX.com/sql_v5i.php?A=1&B=2
您可以使用params=Ticket
在请求中执行相同操作。然后可以使用data=
或files=
发送二进制数据
我使用特殊的门户来测试请求,但您的门户可能需要更多的工作
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"
}
您还可以使用本地代理服务器(ie)来比较PHP请求和Python请求
PHP:
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)
Pyghon是某种类型的库吗?PHP
不发送票证
作为POST数据,而是在url中-请参见“${BASE\u SERVER\u url}?”$postFields
-因此它使用urlhttps://ssl.XXX.com/sql_v5i.php?A=1&B=2
。您可以在requests
中使用params=Ticket
执行相同的操作。然后你可以让我们data=
发送二进制数据。谢谢。这很有帮助。在这个例子中,binary_data=b'hello world'。但在我的例子中,二进制_数据=”����5UU��ǚ���������H�v�������������������........������������������������������" (太长了,无法全部放在注释部分)。当我运行代码时,在文件的第X行(二进制字符串行)显示“SyntaxError:非ASCII字符'\xef',但没有声明编码;有关详细信息,请参阅。如何将此“二进制数据”发送到服务器?
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)