newbie
newbie

Reputation: 607

Having trouble sending file through HTTP (through TCP) in python. What is the issue with my code?

I have created a server in python, and am trying to send a file over to a client when the file is requested. The server receives the request, but then I cannot send the file over through TCP.

I used a template to create a response header, and then I try to send the file afterward, but it does not entirely work. I am able to "send" .py and .html files over, and they do display in my browser, but it must be luck, because according to my TA, the real test is images... which are not working for me.

First I will post the header and response as shown by the Firefox addon Firebug, then my code, and lastly the error message.

Firebug Request and Response

----------------------------

Response Headersview source

Accept-Ranges   bytes
Connection  Keep-Alive (or Connection: close)Content-Type: text/html; charset=ISO-8859-1
Content-Length  10000
Keep-Alive  timeout=10, max=100

Request Headersview source

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Host    xxx.xxx.244.5:10000
User-Agent  Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0

** My python code: **

#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
serverPort = 10000
serverName = 'xxx.xxx.xxx.xx' #Laptop IP
serverSocket.bind((serverName,serverPort))
serverSocket.listen(5)

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

    try:
        message = connectionSocket.recv(4096)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
        f.close()
        print 'length of output data: '
        print len(outputdata)
        print filename
        print message
        header = ("HTTP/1.1 200 OK\r\n"
        "Accept-Ranges: bytes\r\n"
        "Content-Length: 100000\r\n"
        "Keep-Alive: timeout=10, max=100\r\n"
        "Connection: Keep-Alive\r\n (or Connection: close)"
        "Content-Type: text/html; charset=ISO-8859-1\r\n"
        "\r\n")
        connectionSocket.send(header)
        #Send the content of the requested file to the client
        for i in range(0, len(outputdata)):
                connectionSocket.sendall(outputdata[i])         
        connectionSocket.close()


        print '\ntry code has executed\n'

    except IOError:
        print 'exception code has been executed'
        connectionSocket.send('HTTP/1.1 404 Not found: The requested document does not exist on this server.')
        connectionSocket.send('If you can read this, then the exception code has run')
        print '\tconnectionSocket.send has executed'
        connectionSocket.close()
        print '\tconnectionSocket.close has executed\n'
#serverSocket.close()

And here is the error message:

This image "http://xxx.xxx.244.5:10000/kitty.jpg" cannot be displayed because it contains errors.

Thank you in advance!

Upvotes: 0

Views: 2729

Answers (1)

kindall
kindall

Reputation: 184280

Open your JPEG file in binary mode: open(filename[1:], "rb"). Otherwise Python will helpfully translate some bytes in the file to \n characters, which will corrupt the image and prevent the browser from being able to make any sense of it.

Also, you should use a Content-Type of image/jpeg for a JPEG image, rather than text/html, although your browser seems to have figured out that it's a JPEG anyway.

Upvotes: 2

Related Questions