小编Lea*_*ner的帖子

Python 3.x BaseHTTPServer或http.server

我正在尝试制作BaseHTTPServer程序.我更喜欢使用Python 3.3或3.2.我发现该文档很难理解导入的内容,但尝试更改导入:

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
Run Code Online (Sandbox Code Playgroud)

至:

from http.server import BaseHTTPRequestHandler,HTTPServer
Run Code Online (Sandbox Code Playgroud)

然后导入工作和程序启动并等待GET请求.但是当请求到达时会引发异常:

File "C:\Python33\lib\socket.py", line 317, in write return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)

问题:是否有一个版本的BaseHTTPServer或http.server与Python3.x开箱即用,或者我做错了什么?

这是我尝试在Python 3.3和3.2中运行的"我的"程序:

#!/usr/bin/python
# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from http.server import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8080

# This class will handle any incoming request from
# a browser 
class myHandler(BaseHTTPRequestHandler):

    # Handler for the GET requests
    def do_GET(self):
        print   ('Get request received')
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write("Hello World …
Run Code Online (Sandbox Code Playgroud)

python basehttpserver

48
推荐指数
3
解决办法
6万
查看次数

标签 统计

basehttpserver ×1

python ×1