Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 简单TCP服务器无法连接_Python_Networking_Server - Fatal编程技术网

Python 简单TCP服务器无法连接

Python 简单TCP服务器无法连接,python,networking,server,Python,Networking,Server,我正在学习网络入门课程,我们正在学习如何编程一个基本的TCP服务器 我的设置: 分配调用一个服务器,该服务器是一个web服务器,一次处理一个HTTP请求。我的web服务器应该“接受并解析HTTP请求,从服务器的文件系统获取请求的文件,创建一条HTTP响应消息,该消息由请求的文件组成,前面有标题行,然后直接将响应发送到客户端。如果请求的文件不在服务器中,服务器应该发送HTTP响应“404未找到”消息返回给客户端。“我用Python编写了服务器代码(见下文),据我所知,代码是准确的。在同一个目录中,

我正在学习网络入门课程,我们正在学习如何编程一个基本的TCP服务器

我的设置: 分配调用一个服务器,该服务器是一个web服务器,一次处理一个HTTP请求。我的web服务器应该“接受并解析HTTP请求,从服务器的文件系统获取请求的文件,创建一条HTTP响应消息,该消息由请求的文件组成,前面有标题行,然后直接将响应发送到客户端。如果请求的文件不在服务器中,服务器应该发送HTTP响应“404未找到”消息返回给客户端。“我用Python编写了服务器代码(见下文),据我所知,代码是准确的。在同一个目录中,我还创建了一个简单的hello world html文件,因此我需要一些请求

运行我的服务器: 当我运行代码时,终端预期“ready to serve…”消息并侦听连接。这是正确的

然后,当我在浏览器中键入URL时(我已经尝试了这两种方法),浏览器会说它无法连接

我很确定我不是作为客户机正确地连接到服务器,就是如果我的windows计算机没有正确设置,但是如果您能就如何连接并将我的请求发送到服务器提出建议,我将不胜感激

#import socket module
from socket import *
import sys # In order to terminate the program

serverSocket = socket(AF_INET, SOCK_STREAM)

#Prepare a sever socket
serverPort = 1001
serverSocket.bind(('',serverPort))
serverSocket.listen(1)

while True:
    #Establish the connection
    print('Ready to serve...')
    connectionSocket, addr = serverSocket.accept()
    try:
        message = connectionSocket.recv(1024).decode()
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        #Send one HTTP header line into socket
        #Fill in start
        header = 'HTTP/1.1 200 OK\n'
        connectionSocket.send(header.encode())

        #Fill in end

        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i].encode())

        connectionSocket.send("\r\n".encode())
        connectionSocket.close()
    except IOError:
        #Send response message for file not found (404)
        #Fill in start
        error = 'HTTP/1.1 404 Not Found'
        connectionSocket.send(error.encode())
        #Fill in end

        #Close client socket
        #Fill in start
        connectionSocket.close()
        #Fill in end

serverSocket.close()
sys.exit()  #Terminate the program after sending the corresponding data

我刚刚试着运行你的服务器,它工作正常。我试着使用url“”获取python代码本身,然后我得到了你的代码


您可能需要检查的是,您是否有权访问服务器的端口1001。由于这不是标准端口,某些主机可能会阻止它。您可以尝试使用端口80来代替它。

运行服务器时是否出现绑定错误?您应该有。1001是一个保留端口,与1024以下的所有端口一样。这取决于他的环境ows我在非管理员命令行上运行他的脚本没有问题。谢谢你的建议。经过一些修补,我发现如果我使用端口65535并在0.0.0.0中为我的主机交换,那么我可以特别获得Microsoft Edge(有趣的是,不是Chrome)分支到我的HTTP/1.1 404 Not Found error。这至少是一个开始,尽管我不完全清楚为什么这组因素会起作用,我特别想知道为什么chrome不起作用。我的Try分支中的错误似乎在'filename=message.split()[1]声明。这很奇怪,因为该声明是由教授在实验室的基本代码中提供的。