通过URL发送数据但未显示

时间:2014-07-02 13:12:13

标签: python

我有3个脚本:htmlTemplate.py,info.py和hello_get.py

这里是他们的代码:

htmlTempate.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-



from string import Template

user = getpass.getuser()

info_html = """

<html>
<head>
<title>- Project -</title>
</head>
<body>
<h2><center> welcome </center></h2><br/><br/><br/>
Application : ${code_name}
<br/><br/>
code: ${code_version}
<br/><br/>
path install :  ${install_path}
<br/><br/>
os : ${os}
<br/><br/>
unix user :  ${user_name}
<br/><br/>
<form name="sendData" method="get" action="/cgi/hello_get.py">
Nom : <input type="text" name="nom">  <br/><br/>

mail : <input type="text" name="mail" /> <br/><br/>

D&eacutepartement : <input type="text" name="departement" /> <br/><br/>

Projet : <input type="text" name="projet" /> <br/><br/>
<input type="submit" value="OK" />


</form>
</body>
</html>

"""

info_html = Template(info_html)

info.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, platform, getpass, tempfile
import webbrowser
import htmlTemplate

#user = getpass.getuser()

def main( server_IP, code_name, code_version, install_path):
  user = getpass.getuser()

  contenu = htmlTemplate.info_html.substitute(
            code_name = code_name,
            code_version = code_version,
            install_path = install_path,
            os = sys.platform,
            user_name = user)


  url = "info.html"
  f = tempfile.NamedTemporaryFile(prefix='/tmp/info.html', mode='w', delete=False)
  f.write(contenu)
  f.close()

  webbrowser.open(url)

if __name__ == "__main__":
  server_IP = sys.argv[1]
  code_name = sys.argv[2]
  code_version = sys.argv[3]
  install_path = sys.argv[4]
  main(server_IP, code_name, code_version, install_path )

hello_get.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-


import cgi, cgitb, os, sys, socket, getpass, platform
import htmlTemplate, info

form = cgi.FieldStorage() 


#dir_path = os.path.dirname(os.path.abspath(__file__))
#hostname = socket.gethostname()
machine = cgi.escape(os.environ["REMOTE_ADDR"]) 
#user = getpass.getuser()

name = form.getvalue('name')
mail  = form.getvalue('mail')
department = form.getvalue('department')  
project = form.getvalue('project')

print "Content-type:text/html\n\n"
print "<!DOCTYPE html>"
print "<html>"
print "<head>"
print "<title>User data</title>"
print "</head>"
print "<body>"
print "<h2><center> Resume </center></h2><br/><br/><br/>"


print "Your data : "

print "install path is : ", install_path
print "<br/><br/>"

print "OS is :", os
print "<br/><br/>"

print "adress IP is : ", machine
print "<br/><br/>"

print 'Unix user is : ', user
print "<br/><br/>"

print " your name is : %s " % (nom)
print "<br/><br/>" 

print "your mail is : %s " % (mail)
print "<br/><br/>"

print "your department is : %s " % (departement)
print "<br/><br/>"

print "your project is : %s" % (projet)


print "</body>"

print "</html>"

代码服务器:

#!/usr/bin/env python

import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable()  ## This line enables CGI error reporting

server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = ["/cgi"]

httpd = server(server_address, handler)
httpd.serve_forever()

当我像这样执行info.py时:

./info.py server software version install_path

它打开我的webbrowser并显示数据:OK。

当我输入OK按钮时,它打开一个新的网页(正常)url包含由OK(get method)按钮支持发送的数据,如下所示:

file:///cgi/hello_get.py?nom=henry&mail=henry%40gmail.com&departement=IT&projet=stab

但是webbrowser显示:

Firefox can not find the file at / cgi / hello_get.py? Henry & name = & email = henry@gmail.com department = IT & project = stab.

错误不应该出名,但我看了我的代码。

1 个答案:

答案 0 :(得分:0)

保存:

import sys, json 

config_file = open("config.txt", "w") 
json.dump(sys.argv, config_file)

负载:

 import json

 config_file = open("config.txt", "r") 
 my_argv = json.load(config_file) 

 print my_argv

修改

<强> /cgi/info.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import platform
import getpass
import tempfile
import webbrowser
import string
import json 

def main( server_IP, code_name, code_version, install_path, user_os, user_login):

    template = open('htmlTemplate.phtml').read()

    contenu = string.Template(template).substitute(
            code_name = code_name,
            code_version = code_version,
            install_path = install_path,
            os = user_os,
            user_name = user_login
        )

    f = tempfile.NamedTemporaryFile(prefix='/tmp/info.html.', mode='w', delete=False)
    f.write(contenu)
    f.close()

    webbrowser.open(f.name)

if __name__ == "__main__":
    server_IP = sys.argv[1]
    code_name = sys.argv[2]
    code_version = sys.argv[3]
    install_path = sys.argv[4]

    user_os = sys.platform
    sys.argv.append(user_os)

    user_login = getpass.getuser()  
    sys.argv.append(user_login)

    config_file = open("config.txt", "w") 
    json.dump(sys.argv, config_file)

    main(server_IP, code_name, code_version, install_path, user_os, user_login)

<强> /cgi/htmlTemplate.phtml

我只有html和变量名 - 所以我称之为p(ython)html

<html>

<head>
    <title>- Project -</title>
</head>

<body>

<h2><center> welcome </center></h2>
<br/><br/><br/>

Application: ${code_name}<br/><br/>

code: ${code_version}<br/><br/>

path install: ${install_path}<br/><br/>

os: ${os}<br/><br/>

unix user: ${user_name}<br/><br/>

<form name="sendData" method="get" action="http://localhost:8000/cgi/hello_get.py">
    Nom: <input type="text" name="nom"><br/><br/>
    mail: <input type="text" name="mail"/><br/><br/>
    D&eacutepartement: <input type="text" name="department"/><br/><br/>
    Projet: <input type="text" name="project"/> <br/><br/>
    <input type="submit" value="OK"/>
</form>

</body>

</html>

<强> /cgi/hello_get.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import cgi
import cgitb
import os
import sys
import socket
import getpass
import platform

import json

config_file = open("cgi/config.txt", "r") 
my_argv = json.load(config_file) 

form = cgi.FieldStorage() 

machine = cgi.escape(os.environ["REMOTE_ADDR"]) 

nom = form.getvalue('nom')
mail = form.getvalue('mail')
department = form.getvalue('department')  
project = form.getvalue('project')

print "Content-type:text/html\n\n"
print "<!DOCTYPE html>"
print "<html>"
print "<head>"
print "<title>User data</title>"
print "</head>"
print "<body>"
print "<h2><center> Resume </center></h2><br/><br/><br/>"
print "Your data : "
print "install path is : ", my_argv[4]
print "<br/><br/>"
print "OS is :", my_argv[5]
print "<br/><br/>"
print "adress IP is : ", machine
print "<br/><br/>"
print 'Unix user is : ', my_argv[6]
print "<br/><br/>"
print "your name is :", nom
print "<br/><br/>" 
print "your mail is :", mail
print "<br/><br/>"
print "your department is :", department
print "<br/><br/>"
print "your project is :", project
print "</body>"
print "</html>"

<强> /server.py

#!/usr/bin/env python 

import BaseHTTPServer 
import CGIHTTPServer 
import cgitb; cgitb.enable() 

server = BaseHTTPServer.HTTPServer 
handler = CGIHTTPServer.CGIHTTPRequestHandler 

server_address = ("", 8000) 
handler.cgi_directories = ["/cgi"] 
httpd = server(server_address, handler) 
httpd.serve_forever()