简体   繁体   English

如何在Python中打开HTML文件

[英]How do I open an HTML file in Python

I've tried various different ways to open an HTML file from python code, but each time I get a '500 internal server error'. 我尝试了各种不同的方法来从python代码打开HTML文件,但是每次出现“ 500内部服务器错误”时,

Here is my python script: 这是我的python脚本:

    if (variable == "0, User and IP logged"): #conditional to check if user's credentials were accepted by the API
        page = urllib.urlopen("mainPage.html").read()
        print page
        #html file is opened and read - NOT WORKING!

Here is my html file: 这是我的html文件:

    <html>
    <header><title>This is title</title></header>
    <body>
    Hello world
    </body>
    </html>

How do I get the python script to display my hello world page? 如何获取python脚本来显示我的世界页面? My tutor said I should use open(), but I'm not sure how. 我的导师说我应该使用open(),但是我不确定如何使用。

试试这个...

webbrowser.open_new_tab('helloworld.html')

First of all you need to run Web Server which will server web pages : 首先,您需要运行将为网页提供服务器的Web服务器:

Here I am using sample bottle web framework 在这里我正在使用示例瓶子Web框架

bottle_server.py bottle_server.py

from bottle import route, run

@route('/')
def dashboard():
    return '<b>This is index page</b>'

run(host='localhost', port=8080)

Run script using 使用运行脚本

python bottle_server.py python bottle_server.py

Now, Server is running on localhost on port 8080 现在,服务器正在端口8080的本地主机上运行

Here is sample python script 这是示例python脚本

Client.py 客户端

import urllib
print urllib.urlopen('http://localhost:8080/").read()

Run client script using 使用运行客户端脚本

python Client.py python Client.py

This will produce output like 这将产生如下输出

<b>This is index page</b>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM