首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python not服务器不工作

Python not服务器不工作
EN

Stack Overflow用户
提问于 2017-10-02 03:58:43
回答 1查看 124关注 0票数 1

我正在构建一个基本的python web服务器,但我一直有一个问题,它不能发送任何数据(顺便说一句,我在运行它的同一台计算机上访问网站,并且我有服务器试图访问的文件)以下是我的代码:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2017-10-05 14:21:27

问题是服务器是用Python3实现的,但代码混合了字节和字符串,这在Python2中有效,但在Python3中不起作用。

这会在being函数中导致错误,因为partition是在bytes对象上调用的,但提供了str分隔符的值。

代码语言:javascript
运行
复制
>>> 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作为参数)。

此外,打印出发生的任何异常,以便您可以对其进行调试。

代码语言:javascript
运行
复制
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)
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46516347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档