如何使用 web.py 模块将 POST 参数访问到 python 网络服务器?
How to access POST parameters into python web server using web.py module?
我写的 python 脚本是这样的:
import web
import commands
urls = ('getprint', 'GetPrint', 'postprint', 'PostPrint')
app = web.application(urls, globals())
class GetPrint:
def GET(self):
return "Hello, this is GetPrint function :D"
class PostPrint:
def POST(self):
# I don't know how to access to post data!!!
if __name__ == "__main__": app.run()
我想将此脚本用作 Web 服务,并通过来自另一台计算机的 php 脚本调用它。我的 php 调用 python 网络服务的脚本是这样的:
<?php ...
require('CallRest.inc.php');
...
$status = CallAPI('GET', "http://WEBSERVICE_MACHINE_IP:PORT/".$arg);
echo $status;
...
$data = array("textbox1" => $_POST["textbox1"]);
CallAPI('POST', 'http://WEBSERVICE_MACHINE_IP:PORT/'.$arg, $data);
... ?>
而头文件'CallRest.inc.php'是:
<?php
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
class GetPrint
工作正常,但我不知道如何将 post 参数传递给 python 网络服务以及如何将它们访问到 class PostPrint
.
要在您的 Python 代码中访问 POST 数据,您应该在 def POST(self)
之后立即定义一个变量,例如 data = web.input()
。然后就可以访问如图所示的字段here,例如:data.param1
、data.name
等。所以你的代码应该如下所示:
class PostPrint:
def POST(self):
data = web.input()
param1 = data.param1
name = data.name
...
我写的 python 脚本是这样的:
import web
import commands
urls = ('getprint', 'GetPrint', 'postprint', 'PostPrint')
app = web.application(urls, globals())
class GetPrint:
def GET(self):
return "Hello, this is GetPrint function :D"
class PostPrint:
def POST(self):
# I don't know how to access to post data!!!
if __name__ == "__main__": app.run()
我想将此脚本用作 Web 服务,并通过来自另一台计算机的 php 脚本调用它。我的 php 调用 python 网络服务的脚本是这样的:
<?php ...
require('CallRest.inc.php');
...
$status = CallAPI('GET', "http://WEBSERVICE_MACHINE_IP:PORT/".$arg);
echo $status;
...
$data = array("textbox1" => $_POST["textbox1"]);
CallAPI('POST', 'http://WEBSERVICE_MACHINE_IP:PORT/'.$arg, $data);
... ?>
而头文件'CallRest.inc.php'是:
<?php
// Method: POST, PUT, GET etc
// Data: array("param" => "value") ==> index.php?param=value
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
class GetPrint
工作正常,但我不知道如何将 post 参数传递给 python 网络服务以及如何将它们访问到 class PostPrint
.
要在您的 Python 代码中访问 POST 数据,您应该在 def POST(self)
之后立即定义一个变量,例如 data = web.input()
。然后就可以访问如图所示的字段here,例如:data.param1
、data.name
等。所以你的代码应该如下所示:
class PostPrint:
def POST(self):
data = web.input()
param1 = data.param1
name = data.name
...