【发布时间】:2017-08-22 04:59:12
【问题描述】:
我想从 php 发送 post 请求到 python 并得到答案 我写了这个发送帖子的脚本
$url = 'http://localhost:8080/cgi-bin/file.py';
$body = 'hello world';
$options = array('method'=>'POST',
'content'=>$body,
'header'=>'Content-type:application/x-ww-form-urlencoded');
$context = stream_context_create(array('http' => $options));
print file_get_contents($url, false,$context);
我正在使用自定义 python 服务器
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8080)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()
以及接受post请求的python脚本
print('Content-type: text/html\n')
import cgi
form = cgi.FieldStorage()
text2 = form.getfirst("content", "empty")
print("<p>TEXT_2: {}</p>".format(text2))
然后我得到
write() argument must be str, not bytes\r\n'
如何解决? P.S对不起我的英语不好
【问题讨论】:
-
您可能想使用
cURL[php.net/manual/en/book.curl.php],或者如果您的代码更多地构建到类中(可能在下面有一个框架),那么我会看看GuzzleHttp[ docs.guzzlephp.org/en/latest/]图书馆 -
这是一个关于如何使用 cURL 的示例 [davidwalsh.name/curl-post]