简体   繁体   English

简单的 TCP 服务器无法连接

[英]Simple TCP Server unable to connect

I'm in my intro to networking course and we are learning how to program a basic TCP server.我正在学习网络入门课程,我们正在学习如何编写基本的 TCP 服务器。

My Setup: The assignment calls for a server that web server that handles one HTTP request at a time.我的设置:该任务需要一台服务器,该服务器一次处理一个 HTTP 请求。 My web server should "accept and parse the HTTP request, get the requested file from the server's file system, create an HTTP response message consisting of the requested file preceded by header lines, and then send the response directly to the client. If the requested file is not present in the server, the server should send an HTTP “404 Not Found” message back to the client."我的 Web 服务器应该“接受并解析 HTTP 请求,从服务器的文件系统中获取请求的文件,创建由请求的文件和标题行开头的 HTTP 响应消息,然后将响应直接发送到客户端。如果请求文件不存在于服务器中,服务器应向客户端发送 HTTP“404 Not Found”消息。” I have the server coded in Python (see below) and as far as I can tell, the code is accurate.我用 Python 编写了服务器(见下文),据我所知,代码是准确的。 In the same directory I have also created a simple hello world html file so I have something to request.在同一个目录中,我还创建了一个简单的 hello world html 文件,所以我有一些东西要请求。

Running my server: When I run the code, the terminal expected 'ready to serve...' message and listens for a connection.运行我的服务器:当我运行代码时,终端期望“准备服务...”消息并侦听连接。 This is correct.这是对的。

Then when I type in a URL in my browser (I've tried both http://localhost:1001 and http://localhost:1001/HelloWorld.html ) the browser says its unable to connect.然后,当我在浏览器中输入 URL 时(我已经尝试了http://localhost:1001http://localhost:1001/HelloWorld.html ),浏览器说它无法连接。

Unable to connect screen from browser无法从浏览器连接屏幕

I'm pretty sure I'm either not connecting to the server properly as a client or if my windows machine isn't set up properly but any advise on how to connect and get my request through to the server would be greatly appreciated.我很确定我要么没有作为客户端正确连接到服务器,要么我的 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

I just tried running your server and it worked properly.我刚刚尝试运行您的服务器,它运行正常。 I tried getting the python code itself using the url " http://localhost:1001/test.py " and I got back your code.我尝试使用 url“ http://localhost:1001/test.py ”获取 python 代码本身,然后我取回了你的代码。

Some things you might want to check is if you have access to your server's port 1001. Since this is not a standard port, some hosts could block it.您可能要检查的一些事情是您是否有权访问服务器的端口 1001。由于这不是标准端口,因此某些主机可能会阻止它。 You could try using port 80 instead.您可以尝试改用端口 80。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM