Файл расширения CGI не может быть выполнен в Python HTTPServer - PullRequest
0 голосов
/ 31 марта 2019

Я отправляю URL-адрес (http://192.168.0.139:10000/smartplug.cgi) через request.post на сервер. Сервер может получить 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")

введите описание изображения здесь

...