简体   繁体   English

Python Socket编程简单 Web服务器

[英]Python Socket Programming Simple Web Server

I am working on my first Python socket programming code and I cannot figure out what is wrong.我正在编写我的第一个 Python 套接字编程代码,但我不知道哪里出了问题。 I type in the IP address of the server that this program is running on along with the port number and the file I am trying to receive.我输入运行此程序的服务器的 IP 地址以及端口号和我试图接收的文件。 I should receive the file in the browser and the socket should close.我应该在浏览器中收到文件并且套接字应该关闭。 Instead, the server prints out the print line 'Ready to serve...' three times, displays '404 Not Found' on the browser, and never closes the socket.取而代之的是,服务器将打印行“Ready to serve...”打印了三次,在浏览器上显示“404 Not Found”,并且从不关闭套接字。 Does anyone have any ideas?有人有什么想法吗?

#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() 

Thank you everyone for all the help.谢谢大家的帮助。 I figured out what was wrong.我想出了什么问题。 I had renamed my HTML to "HelloWorld.html" and then Windows automatically added .html to end of the file.我已将 HTML 重命名为“HelloWorld.html”,然后 Windows 自动将 .html 添加到文件末尾。 So in order to have accessed the file I would of needed to type in HelloWorld.html.html.因此,为了访问该文件,我需要输入 HelloWorld.html.html。 I changed the file name and then this code worked perfectly.我更改了文件名,然后此代码完美运行。

This code should work:此代码应该工作:

# 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() 

Before this line filename = message.split()[1]在此行之前filename = message.split()[1]

print(message)

after this line filename = message.split()[1]在这一行之后filename = message.split()[1]

print(filename)

I thought that the error is in that line.我认为错误在该行中。

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

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