代码之家  ›  专栏  ›  技术社区  ›  Lehtim

在python中打开html:urllib。错误URLError:<urlopen error no host gived>

  •  0
  • Lehtim  · 技术社区  · 8 年前

    我在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公司。形态:

    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里面的一些东西。配置,但我不知道是什么。谢谢你的帮助。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Daniel Roseman    8 年前

    这里有几件事不对。

    第一 "localhost//my_html" 不是URL。你至少需要一个协议,而且你不想删减。 "http://localhost/my_html" 更像是这样。

    然而,这并不能满足您的需要。它会下载任何文件,但不会对其做任何操作;它当然不会为您“在浏览器中打开”。

        2
  •  0
  •   Xero Smith    8 年前

    您的错误源于 r.urlopen("localhost//my_html") 。的第一个参数 urllib.request.urlopen 应为有效的url。这意味着包含协议(http或https)的完整url。