2

Python Webサーバーを実行しようとしています。サーバーは実行中であると言っているので、htmlテキストしか表示されないことを除いて、すべて機能していると思います。jpeg/image および pdf ファイルは表示されません。これが私がこれまでに持っているものです。

#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 に答える