启用Webiopi CORS请求

时间:2018-11-24 10:04:58

标签: python angular http webiopi

我想在运行Raspberry的浏览器中从我的角度应用程序调用Webiopi REST API。由于Webiopi HTTP服务器不允许CORS请求,因此我使用apache创建了一个代理,该代理发送Header add "Access-Control-Allow-Origin" "*"头。

这很好,但是对REST API的调用引发了许多错误,主要是因为在CORS请求检查是否允许的情况下,浏览器向服务器发送了OPTIONS请求。但是webiopi http处理程序根本不处理OPTIONS动词。

因此,我以零Python经验开始自己将其写入代码。在文件python / webiopi / protocols / http.py的最后,我添加了:

def do_OPTIONS(self):
    self.send_response(200,"ok")
    self.send_header("Access-Control-Allow-Origin", "*")
    self.send_header("Access-Control-Allow-Methods", "POST,GET,OPTIONS")
    self.send_header("Access-Control-Allow-Headers", "Authorization")
    self.send_header("Access-Control-Allow-Headers", "Content-Type")
    self.end_headers()

现在,它不会引发任何错误,但是不能正确响应我的GET请求。它仅在OPTIONS之后停止。请求和响应如下所示:

请求标头:

OPTIONS /GPIO/1/value HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://192.168.1.108:51443
User-Agent: Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: hu-HU,hu;q=0.9,en-US;q=0.8,en;q=0.7

响应标题:

HTTP/1.1 200 OK
Date: Fri, 23 Nov 2018 22:06:28 GMT
Server: WebIOPi/0.7.1/Python3.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked

常规(从Chrome网络标签中):

Request URL: http://localhost:8000/GPIO/1/value
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: [::1]:8000
Referrer Policy: no-referrer-when-downgrade

我的GET请求在哪里?为什么我只能看到根本不启动的OPTIONS?

来自角度的请求:

this.http.get<number>(this.route+'GPIO/'+gpio+'/value').subscribe(result => {
                resolve(result);
            })

1 个答案:

答案 0 :(得分:0)

我必须启用http服务器的所有标头:

import os
from tkinter import filedialog
from tkinter import *

root = Tk()
root.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select 
file",filetypes = (("python","*.py"),("all files","*.*")))
print(root.filename)

a = root.filename

print(a.rfind('/'))
b = 'python '
c = (a[(a.rfind('/')+1):])
d = (b+c)

print(d)
os.system(d)