CGI扩展文件无法在HTTPServer Python中执行

时间:2019-03-31 01:27:06

标签: python cgi httpserver

我正在通过request.post发送一个URL(http://192.168.0.139:10000/smartplug.cgi)到服务器。服务器能够接收该URL,但是如何执行该URL。

  1. 在浏览器中手动测试url时,它不会执行,而是要求下载文件。我已经执行了所有建议,但是结果是一样的。

  2. 如何通过代码调用/执行cgi url ??

================================================ =======================

客户:

def get_smartplug(self, smartplug_IP):
PORT_HTTP = 10000 
payload = "<tes>ddd</tes>" 
smartplug_url = "http://" + str(smartplug_IP)+":"+str(PORT_HTTP)
smartplug_url_cgi = "http://" +   str(smartplug_IP)+":"+str(PORT_HTTP)+"/smartplug.cgi"
headers ={"Location" : smartplug_url_cgi} 
r = requests.post(smartplug_url, data=payload, headers=headers, auth=HTTPBasicAuth('admin','1234'))'

服务器:

class Handler(CGIHTTPRequestHandler):
def do_POST(self):
    message = plug_status

    cgi_url = self.headers["Location"]
    print(cgi_url)  #<--the url received
    userpwd = self.headers['Authorization']
    userpwd_decoded = base64.b64decode(userpwd[6:len(userpwd)]).decode()
    print(userpwd_decoded)
    content_length = int(self.headers['Content-Length'])  # <--- Gets the size of data
    post_data = self.rfile.read(content_length)  # <--- Gets the data itself
    payload = post_data.decode(ENDCODING)
    print(payload)
    self._set_response()

def start_http(self):
    hPort = 10000
    Handler.cgi_directories=[""]
    httpd = HTTPServer((self.IP, hPort), Handler)
    httpd.serve_forever()

smartplug.cgi:
#!/usr/bin/env python

import cgi
import cgitb

cgitb.enable()

print("Content-Type: text/html\n\n')
print("Hello world")

enter image description here

0 个答案:

没有答案