我正在尝试将数据从我的PHP脚本传递到我的Python脚本,以便将数据从Python写入XML文件。
PHP脚本:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['submit']))
{
#--Fetching data from the Form
$fserverId = $_POST['field1'];
$finsname = $_POST['field2'];
$fhost = $_POST['field3'];
$fport = $_POST['field4'];
$fproto = $_POST['field5'];
$fuserName = $_POST['field6'];
$fpassword = $_POST['field7'];
$fkey = $_POST['field8'];
$fcompanyName = $_POST['field9'];
$fofficeAddress = $_POST['field10'];
$fstate = $_POST['field11'];
$fcountry = $_POST['field12'];
$fladmin = $_POST['field13'];
$fphone = $_POST['field14'];
$fmobile = $_POST['field15'];
$femail = $_POST['field16'];
$fdesignation = $_POST['field17'];
$frManager = $_POST['field18'];
$data = " --serverId ".$fserverId." --name ".$finsname." --host ".$fhost." --port ".$fport." --proto ".$fproto." --username ".$fuserName." --password ".$fpassword." --key ".$fkey." --companyName ".$fcompanyName." --officeAddress ".$fofficeAddress." --state ".$fstate." --country ".$fcountry." --ladmin ".$fladmin." --phone ".$fphone." --mobile ".$fmobile." --email ".$femail." --designation ".$fdesignation." --rManager ".$frManager;
#--Parsing the data from PHPscript to CGI
echo "Firewall Creation:: ".$data;
$output = exec('python3 /var/www/cgi-bin/dscr.cgi' .$data);
echo "<pre>hello -------</pre>";
echo "<pre>$output</pre>";
}
?>
Python脚本:
import cgi, cgitb
import xml.etree.ElementTree as et
import os
from xml.dom import minidom
import sys
import argparse
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings()
#--ADDING THE XML FILE HERE
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, "company.xml")
form = cgi.FieldStorage()
print (xml_file)
#--PARSING THE ARGUMENTS
#--To be used to get arguments from Command Line or Php Script
parser = argparse.ArgumentParser(description ='parser script')
parser.add_argument('--serverId', required=True, help='Server ID to find device info')
parser.add_argument('--name', required=True, help='Name Of the Policy')
parser.add_argument('--host', default='any', help='Host of the IP Address')
parser.add_argument('--port', default='any', help='Port used')
parser.add_argument('--proto', default='any', help='Protocol used')
parser.add_argument('--username', default='any', help='Username')
parser.add_argument('--password', default='any', help='Password')
parser.add_argument('--key', default='any', help='')
parser.add_argument('--companyName', default='any', help='Name of the Company')
parser.add_argument('--officeAddress', default='any', help='Address of the Company')
parser.add_argument('--state', default='any', help='State')
parser.add_argument('--country', default='any', help='Country')
parser.add_argument('--ladmin', default='any', help='Local Admin')
parser.add_argument('--phone', default='any', help='Phone Number')
parser.add_argument('--mobile', default='any', help='Mobile Number')
parser.add_argument('--email', default='any', help='Email Address')
parser.add_argument('--designation', default='any', help='Designation of the concerned')
parser.add_argument('--rManager', default='any', help='Reporting Manager of the concerned Person')
args = parser.parse_args()
#--Associating variables to parsed arguments
name = args.name
host = args.host
port = args.port
proto = args.proto
user = args.username
password = args.password
key = args.key
company = args.companyName
officeAddr = args.officeAddress
state = args.state
country = args.country
ladmin = args.ladmin
phone = args.phone
mobile = args.mobile
email = args.email
designation = args.designation
reportingMgr = args.rManager
#--Retrieving the element tree from XML file
tree = et.parse(xml_file)
root = tree.getroot()
#--Creating Elements and input value
new_name = et.Element("name")
new_name.text = name
new_host = et.Element("host")
new_host.text = host
new_port = et.Element("port")
new_port.text = port
new_proto = et.Element("proto")
new_proto.text = proto
new_user = et.Element("user")
new_user.text = user
new_password = et.Element("password")
new_password.text = password
new_key = et.Element("key")
new_key.text = key
new_companyName = et.Element("companyName")
new_companyName.text = company
new_address = et.Element("address")
new_officeAddr = et.Element("officeAddr")
new_officeAddr.text = officeAddr
new_state = et.Element("state")
new_state.text = state
new_country = et.Element("country")
new_country.text = country
new_contact = et.Element("contact")
new_ladmin = et.Element("ladmin")
new_ladmin.text = ladmin
new_phone = et.Element("phone")
new_phone.text = phone
new_mobile = et.Element("mobile")
new_mobile.text = mobile
new_email = et.Element("email")
new_email.text = email
new_designation = et.Element("designation")
new_designation.text = designation
new_reportingMgr = et.Element("reportingManager")
new_reportingMgr.text = reportingMgr
new_address.append(new_officeAddr)
new_address.append(new_state)
new_address.append(new_country)
new_contact.append(new_ladmin)
new_contact.append(new_phone)
new_contact.append(new_mobile)
new_contact.append(new_email)
new_contact.append(new_designation)
new_contact.append(new_reportingMgr)
new_server = et.Element("server")
new_server.append(new_name)
new_server.append(new_host)
new_server.append(new_port)
new_server.append(new_proto)
new_server.append(new_user)
new_server.append(new_password)
new_server.append(new_key)
new_server.append(new_companyName)
new_server.append(new_address)
new_server.append(new_contact)
root.append(new_server)
str = et.tostring(root)
reparsed = minidom.parseString(str)
tree.write(xml_file)
with open(xml_file, 'w') as output:
output.write(reparsed.toprettyxml(indent="\t"))
在尝试执行时,数据不会写在XML文件上,但是当我在python脚本中通过命令行传递参数时,它会被写入。因此,在两个脚本之间的调用/桥接中必定存在问题,因为我尝试从PHP发送到Python的数据未被解析。