怎么退出cgi?

时间:2015-08-15 18:38:02

标签: python cgi

我编写了一个python-cgi脚本(raspberry),它在浏览器中显示了一些带有一些数据的页面。我将从浏览器中输入常用地址(//localhost/cgi-bin/myscript.py)的其他PC(linux,mac等)进入此页面。该页面将以全屏模式显示。我会在页面上有一个按钮,按下该按钮,页面将关闭并退出浏览器。我尝试使用pygame按钮并使用提交(发布或获取方法)按钮但没有成功(我不是真的很专业!)。 有人可以帮帮我吗? 感谢

#!/usr/bin/python
import os.path, sys, datetime, time
import cgi, cgitb
import os
import RPi.GPIO as io
import pygame, random, pygame.font, pygame.event, pygame.draw, string

import common
pygame.init()
io.setmode(io.BCM)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

ctime = datetime.datetime.now()

clock = ctime.strftime("%H:%M")
date = ctime.strftime("%A %d")

form = cgi.FieldStorage()

fileobj = open('/sys/bus/w1/devices/28-0000065fb5c1/w1_slave','r')
lines1 = fileobj.readlines()
fileobj.close()
Temp2 = int(5)

Temp= form.getvalue(Temp2)

print "Content-type: text/html\n\n"

def main():

        print "<html>"
        print "<head>"
        print "<meta http-equiv='refresh' content='5' >"

        print "</head>"
        print "<body>"
        print "<body bgcolor=""#000000"">"

        fileobj = open('/sys/bus/w1/devices/28-0000065fb5c1/w1_slave','r')
        lines1 = fileobj.readlines()
        fileobj.close()
        Temp2 = int(5)
        if (lines1[0][-4])=='Y':
                pos1=lines1[1].index('t=')
                Temp2 = int(lines1[1][pos1+2:])/1000
                Temp2=round(Temp2,1)
                time.sleep(1)

        print "<table style=""width:1920px"">"
        print "<tr height=""60"">"
        print "</tr>"
        print "<tr height=""400"">"

        print "<td width=""442px"" >"
        print "</td>"
        if Temp2 <= 32:

            print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        elif Temp2 > 32 and Temp2 <= 37:
            print "<td width=""345""  background=""//192.168.10.32/dashgreen.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        if Temp2 > 37:
            print "<td width=""345""  background=""//192.168.10.32/dashred.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2       
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>" 
        file = open('/home/pi/Downloads/temperat.txt','r')
        lines = file.readlines()
        file.close()
        Temp = float(lines[0][:4])
        Temp=round(Temp,1)
        if Temp <= 22:

            print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        elif Temp > 22 and Temp <= 26:
            print "<td width=""345""  background=""//192.168.10.32/dashgreen.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        if Temp > 27:
            print "<td width=""345""  background=""//192.168.10.32/dashred.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp       
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>" 

        Hum=int(lines[1])
        print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print"<p align=""center"">"
        print "<b>"
        print Hum
        degree = chr(37)
        print degree
        print "</b>"
        print "</p>"
        print "</td>"
        print "<td width=""6%"" >"
        print "</font>"
        print "</td>"

        print "</tr>"


        print "<tr height=""80"">"
        print "<td width=""6%"" >"
        print "</td>"
        print "<td width=""345"">" 
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print "</p>"
        print "<p align=""center"">"
        print "Water Temperature"
        print "</p>"
        print "</font>"
        print "</td>"   
        print "<td width=""345"" >"
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print "<p align=""center"">"
        print "Bath Temperature"
        print "</p>"
        print "</font>"
        print "</td>"       

        print "<td width=""345"" >"
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print"<p align=""center"">"
        print "Bath Humidity"
        print "</p>"
        print "</font>"
        print "</td>"
        print "<td width=""442px"" >"
        print "</td>"

        print "</tr>"

        print "</table>"

        print "<font size=""10"" color=""#00FFF"" face=""sans-serif"" >"
        print "<h1 align=""center"">now..%s&nbsp&nbsp&nbsp&nbsp%s</h1>" % (date,clock)

        print "</font>"
        print "</body>"
        print "</html>"
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_F6):
                pygame.quit()
                sys.exit()


if (__name__=='__main__'):
        main()

1 个答案:

答案 0 :(得分:0)

你的问题不是Python的问题,它是一个JavaScript问题,就像@rob所说的那样,它与这个问题重复:stackoverflow.com/questions/2076299/...

并为你总结一下;你需要JavaScript来关闭窗口,但是当它发生时,JavaScript会被沙盒化,以防止它关闭页面上没有被同一个脚本打开的窗口,所以如果你打开一个标签或窗口浏览器并导航到一个页面,该页面不能自杀也不能杀死浏览器,它只能关闭并杀死它通过JavaScript启动的任何窗口。

您唯一能做的就是导航到不同的页面,例如在JavaScript中执行window.location = 'A DIFFERENT URL';您甚至可以导航到about:blank,在大多数浏览器上显示空白页。