我有这个网站的代码:
<html>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="hidden" name="cmd_url" value="netstat -an" />
<input type="submit" value="Go" />
</form>
<?php
if(isset($_POST['cmd_url'])){
$cmd = $_POST['cmd_url'];
$output = shell_exec($cmd);
echo '<pre>'.$output.'</pre>';
}
?>
</body>
</html>
我正在尝试发送一个发布请求并使用 python 中的 urllib 为 cmd_url 传递一个值,并在网络浏览器中的新点击上打开该站点,如下所示:
import urllib, urllib2, cookielib
# do POST to the form
url_2 = 'site.php'
values = dict(cmd_url= 'netstat -an' )
data = urllib.urlencode(values)
#submit the form
req = urllib2.Request(url_2, data)
rsp = urllib2.urlopen(req)
#read the return result
content = rsp.read()
import webbrowser
url = "site.php"
new = 2 #open in new window
webbrowser.open(url,new=new)
该网站在新窗口中打开,但请求不会在浏览器中触发,尽管我能够在终端上打印响应。
python 代码应该在网站页面上显示命令的结果。 有什么想法吗?