我正在构建一个基本的python web服务器,但我一直有一个问题,它不能发送任何数据(顺便说一句,我在运行它的同一台计算机上访问网站,并且我有服务器试图访问的文件)以下是我的代码:
import socket
HOST, PORT = '', 80
def between(left,right,s):
    before,_,a = s.partition(left)
    a,_,after = a.partition(right)
    return a
filereq = ""
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
lines = []
print("Started!")
listen_socket.listen(1)
print("Listening")
while True:
        try:
                lines = []
                client_connection, client_address = listen_socket.accept()
                print("Connected")
                request = client_connection.recv(1024)
                print("Received Data!")
                filereq = between("GET /", " HT", request)
                print(filereq)
                filereq = open(filereq)
                for line in filereq:
                        lines.append(line)
                print(lines)
                sendata = ''.join(lines)
                print(sendata)
                http_response = """\
                HTTP/1.1 200 OK 
                {}
                """.format(sendata)
                print(http_response)
                client_connection.sendall(http_response)
                print("Sent the Data!")
                client_connection.close()
                print("Connection Closed!")
        except:
                5+5发布于 2017-10-05 14:21:27
问题是服务器是用Python3实现的,但代码混合了字节和字符串,这在Python2中有效,但在Python3中不起作用。
这会在being函数中导致错误,因为partition是在bytes对象上调用的,但提供了str分隔符的值。
>>> data = b'abc'
>>> data.partition('b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'要解决此问题,请在从套接字读取数据时将数据从bytes解码为str,然后在发送响应之前编码回bytes (socket.sendall需要bytes作为参数)。
此外,打印出发生的任何异常,以便您可以对其进行调试。
import socket
import sys
import traceback
HOST, PORT = '', 80
def between(left,right,s):
    before,_,a = s.partition(left)
    a,_,after = a.partition(right)
    return a
filereq = ""
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
lines = []
print("Started!")
listen_socket.listen(1)
print("Listening")
while True:
        try:
                lines = []
                client_connection, client_address = listen_socket.accept()
                print("Connected")
                request = client_connection.recv(1024)
                print("Received Data!")
                # Decode the data before processing.
                decoded = request.decode('utf-8')
                filereq = between("GET /", " HT", decoded)
                print(filereq)
                filereq = open(filereq)
                for line in filereq:
                        lines.append(line)
                print(lines)
                sendata = ''.join(lines)
                print(sendata)
                http_response = """\
                HTTP/1.1 200 OK 
                {}
                """.format(sendata)
                print(http_response)
                # Encode the response before sending.
                encoded = http_response.encode('utf-8')
                client_connection.sendall(encoded)
                print("Sent the Data!")
                client_connection.close()
                print("Connection Closed!")
        except Exception:
                # Print the traceback if there's an error.
                traceback.print_exc(file=sys.stderr)https://stackoverflow.com/questions/46516347
复制相似问题