python cgi root运行,如何运行Python CGI脚本

I have never setup a server (let alone a python server) before and i am a bit lost. How do i utilize the following code? I have tried to put in in the cgi bin directory but that didnt work. It returned an internal server error. have a look at this here

#!/usr/bin/env python

#

# Funf: Open Sensing Framework

# Copyright (C) 2010-2011 Nadav Aharony, Wei Pan, Alex Pentland.

# Acknowledgments: Alan Gardner

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

from SocketServer import ThreadingMixIn

import sys

import cgi

import urlparse

import os.path

import shutil

import time

server_dir = os.path.dirname(__file__)

config_path = '/config'

config_file_path = os.path.join(server_dir, 'config.json')

upload_path = '/data'

upload_dir = os.path.join(server_dir, 'uploads')

def read_config():

config = None

try:

with open(config_file_path) as config_file:

config = config_file.read()

except IOError:

pass

return config

def backup_file(filepath):

shutil.move(filepath, filepath + '.' + str(int(time.time()*1000)) + '.bak')

def write_file(filename, file):

if not os.path.exists(upload_dir):

os.mkdir(upload_dir)

filepath = os.path.join(upload_dir, filename)

if os.path.exists(filepath):

backup_file(filepath)

with open(filepath, 'wb') as output_file:

while True:

chunk = file.read(1024)

if not chunk:

break

output_file.write(chunk)

class RequestHandler(BaseHTTPRequestHandler):

def do_GET(self):

parsed_url = urlparse.urlparse(self.path)

if parsed_url.path == config_path:

config = read_config()

if config:

self.send_response(200)

self.end_headers()

self.wfile.write(config)

else:

self.send_error(500)

elif parsed_url.path == upload_path:

self.send_error(405)

else:

self.send_error(404)

def do_POST(self):

parsed_url = urlparse.urlparse(self.path)

path = parsed_url.path

ctype, pdict = cgi.parse_header(self.headers['Content-Type'])

if path == upload_path:

if ctype=='multipart/form-data':

form = cgi.FieldStorage(self.rfile, self.headers, environ={'REQUEST_METHOD':'POST'})

try:

fileitem = form["uploadedfile"]

if fileitem.file:

try:

write_file(fileitem.filename, fileitem.file)

except Exception as e:

print e

self.send_error(500)

else:

self.send_response(200)

self.end_headers()

self.wfile.write("OK")

return

except KeyError:

pass

# Bad request

self.send_error(400)

elif parsed_url.path == config_path:

self.send_error(405)

else:

self.send_error(404)

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):

"""Handle requests in a separate thread."""

if __name__ == '__main__':

if sys.argv[1:]:

port = int(sys.argv[1])

else:

port = 8000

server_address = ('', port)

httpd = ThreadedHTTPServer(server_address, RequestHandler)

sa = httpd.socket.getsockname()

print "Serving HTTP on", sa[0], "port", sa[1], "..."

print 'use to stop'

httpd.serve_forever()

解决方案

If you want to run a CGI on something like Apache (as opposed via custom server code like you pasted above), you can create a .py file like this in a (.py) CGI-enabled directory.

#!/usr/bin/env python

print "Content-Type: text/html"

print

print 'Hello World'

edit: (As Adrien P. says, the Python script should be made executable.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值