我已经在python上编写了一个程序,如果单击了一个按钮,则在浏览器中打开一个html文件。不幸的是,文件可以在浏览器中通过nginx服务器在URL中输入而不是由程序打开。我得到以下错误:
urllib.error.URLError: <urlopen error no host given>
以下是我的文件:
GUI.py:
import tkinter as tk
import tkinter.ttk as ttk
import urllib.request as r
class MainWindow(ttk.Frame):
def __init__(self, parent):
self.parent = parent
ttk.Frame.__init__(self, master=self.parent)
self.parent.grab_set()
self.parent.title("Test Data Tool")
self.parent.geometry("500x500")
ttk.Button(self.parent, text="Show HTML", command=self.show_html).pack()
def show_html(self):
r.urlopen("localhost//my_html")
nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /Home {
alias D:/z003e42d;
index test.html;
}
location /my_html {
alias D:/z003e42d/location/of/myHtml;
index My.html;
# CORS config for nginx
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
也许我错过了nginx.config中的内容,但我不知道是什么。谢谢你的帮助。
答案 0 :(得分:0)
这里有几个问题。
首先,"localhost//my_html"
不是网址。您至少需要一个协议,并且您不希望斜杠。 "http://localhost/my_html"
更像是它。
然而,这不会做你想要的。它将下载该文件,但它不会对它做任何事情;它当然不会为你“在浏览器中打开它”。
答案 1 :(得分:0)
您的错误源于r.urlopen("localhost//my_html")
。 urllib.request.urlopen
的第一个参数应该是一个有效的URL。这意味着包含协议(http或https)的完整URL。