2

I'm trying to run a python web server. The server says is running so I assume it is all working, except that I can only see the html text. The jpeg/image and the pdf files won't dispaly. Here is what i have so far.

#import socket module
from socket import *

serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', 12000))  
serverSocket.listen(1) 

while True:
    print 'Ready to serve...'    
    connectionSocket, addr = serverSocket.accept() 
    print 'Required connection', addr

    try:
        message = connectionSocket.recv(1024) 
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read() 

        #Fill in start
        connectionSocket.send('HTTP/1.0 200 OK\r\n\r\n')
        #Fill in end
        #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
        #Fill in start
        connectionSocket.send('404 Not Found')                       
        #Fill in end
        #Close client socket
        #Fill in start
        connectionSocket.close()                      
        #Fill in end
        serverSocket.close()
4

1 回答 1

0

我正在做同样的事情。我添加了这一行:

  connectionSocket.send('Content-Type: image/jpeg') 

在此之后:

  connectionSocket.send('HTTP/1.0 200 OK')

我假设我/我们只是在标题中缺少了一些行。

于 2013-09-29T05:16:34.273 回答