【问题标题】:Python Socket Programming Simple Web ServerPython Socket 编程 简单的 Web 服务器
【发布时间】:2015-02-14 23:00:29
【问题描述】:

我正在编写我的第一个 Python 套接字编程代码,但我不知道出了什么问题。我输入运行该程序的服务器的 IP 地址以及端口号和我尝试接收的文件。我应该在浏览器中接收文件并且套接字应该关闭。相反,服务器会打印 3 次打印行“Ready to serve...”,在浏览器上显示“404 Not Found”,并且从不关闭套接字。有人有什么想法吗?

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverSocket.bind(('', 12006))
serverSocket.listen(1)
while True:
    print 'Ready to serve...'
    #Establish the connection
    connectionSocket, addr = serverSocket.accept()
    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        f.close()
        #Send one HTTP header line into socket
        connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n')
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
            connectionSocket.send(outputdata[i])
        connectionSocket.close()
    except IOError:
        #Send response message for file not found
        connectionSocket.send('404 Not Found')
        #Close client socket
        connectionSocket.close()
serverSocket.close() 

【问题讨论】:

  • 你怎么知道它永远不会关闭套接字?
  • 尝试打印 IOError 异常对象,这可能会提供更多信息。您可能会从打开的文件中获取 IOError。 Python 2 还是 3?
  • 我已经在 python 2.7.5 和 Windows Vista 中尝试过,它可以工作。
  • @rafalopez79 我使用的是 python 2.7.9。我正在访问的文件应该放在哪里?也许这是我的问题。
  • 我正在测试它:127.0.0.1:12006/test.py 其中 test.py 是你的 python 脚本

标签: python sockets web websocket


【解决方案1】:

感谢大家的帮助。我发现出了什么问题。我已将我的 HTML 重命名为“HelloWorld.html”,然后 Windows 自动将 .html 添加到文件末尾。因此,为了访问该文件,我需要输入 HelloWorld.html.html。我更改了文件名,然后这段代码完美运行。

【讨论】:

    【解决方案2】:

    这段代码应该可以工作:

    # python 3
    from socket import *
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('127.0.0.1', 5500))
    serverSocket.listen(1)
    while True:
        print("Server is running")
        connectionSocket, addr = serverSocket.accept()
        try:
            message = connectionSocket.recv(1024)
            filename = message.split()[1].decode('utf-8').strip("/")
            print(filename)
            f = open(filename)
            outputdata = f.read()
            f.close()
            connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n'.encode())
            for i in range(0, len(outputdata)):
                connectionSocket.send(outputdata[i].encode())
            connectionSocket.close()
        except IOError:
            connectionSocket.send('404 Not Found'.encode())
            connectionSocket.close()
    serverSocket.close() 
    

    【讨论】:

      【解决方案3】:

      在这行之前filename = message.split()[1]

      print(message)
      

      在这行之后filename = message.split()[1]

      print(filename)
      

      我以为错误就在那一行。

      【讨论】:

        猜你喜欢
        • 2019-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多