marmot8 2022-03-26 14:52 采纳率: 25%
浏览 514
已结题

SyntaxError: EOL while scanning string literal

# http://127.0.0.1:900/
from wsgiref.simple_server import make_server

def rj(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    with open(r"L:\HBuilderProjects\stu\index.html", errors="ignore") as file:
        html = file.read()
    return [exec("b'{}'".format(html))]
port = 900
http = make_server('', port, rj)
http.serve_forever()

运行代码,出现如下错误

Traceback (most recent call last):
  File "C:\Users\yc\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "D:/Users/gyc/Desktop/服务器.py", line 6, in rj
    return [exec("b'{}'".format(html))]
  File "<string>", line 1
    b'<!DOCTYPE html>
                    ^
SyntaxError: EOL while scanning string literal
127.0.0.1 - - [26/Mar/2022 14:38:53] "GET / HTTP/1.1" 500
  • 写回答

3条回答 默认 最新

  • marmot8 2022-03-26 17:53
    关注

    解决了!

    from wsgiref.simple_server import make_server
    
    # http://127.0.0.1:900/
    def rj(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        with open(r"L:\HBuilderProjects\stu\index.html", errors="ignore", encoding="utf-8") as file:
            html = file.read()
        return [bytes(html.encode('utf-8'))]
    
    port = 900
    http = make_server('', port, rj)
    http.serve_forever()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 修改了问题 3月26日
  • 创建了问题 3月26日