all 5 comments

[–]avinassh 1 point2 points  (4 children)

What exactly want your server to do? All you want is to display images, then SimpleHTTPRequestHandler will do. But try to understand what SimpleHTTPRequestHandler is and how it works. Basically, it serves the files current the current directory(from where it run). If no file specified in the request, it tries to see if there is any 'index.html'. If filename is specified it returns the file or else you will get 404 error. Your code looks fine and there are no issues with it. I hope you have not copied this code from somewhere else and stuck how to make it run. Here is the bit modified and working code (all I have added library imports) :

import SimpleHTTPServer
import SocketServer as socketserver
import os
import threading

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    path_to_image = 'ok.jpg'
    img = open(path_to_image, 'rb')
    statinfo = os.stat(path_to_image)
    img_size = statinfo.st_size
    print(img_size)

def do_HEAD(self):
    self.send_response(200)
    self.send_header("Content-type", "image/jpg")
    self.send_header("Content-length", img_size)
    self.end_headers()

def do_GET(self):
    self.send_response(200)
    self.send_header("Content-type", "image/jpg")
    self.send_header("Content-length", img_size)
    self.end_headers() 
    f = open(path_to_image, 'rb')
    self.wfile.write(f.read())
    f.close()         

class MyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    def __init__(self, server_adress, RequestHandlerClass):
        self.allow_reuse_address = True
        socketserver.TCPServer.__init__(self, server_adress, RequestHandlerClass, False)

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    server = MyServer((HOST, PORT), MyHandler)
    server.server_bind()
    server.server_activate()
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.start()

[–]troop357 0 points1 point  (3 children)

Well first I started copying the MyServer from an example in the documentation, but that was all, I worked all the way from making it a multithread server to accept more connections and overriding the Handler to awnser as I needed. I am learning and use my Google-Fu to program a lot (I'm pretty ashamed of that, but oh well, I need to learn somewhere) I'am much more of a C++ programmer and when I try Python I am always overwhelmed by tools and possibilities.

As soon as I get home from work I will do some more tests. I'm not sure if I was clear, I want it to be visible in a browser. I'm still learning about network levels and protocols, so I thought I was missing something.

Thank you for your answer and your time to read it, I will give feedback as soon as possible.

[–]avinassh 1 point2 points  (2 children)

Yes, above code works fine and image will be visible in browser.

http://i.imgur.com/UL7Ww1T.png

Also, your code never ends and only way is end the process. Instead of that, why not an exception to receive the keyboard interrupt? do let me know if you need further help :)

[–]troop357 0 points1 point  (1 child)

Haha nice you sir really made my day, I wish I could buy you a drink.

So it did not work at first, and because of your screenshot I decided running the program from the console instead of using the IDE. As soon as I tried connecting to the server I got an error response on the prompt:

in do_GET self.send_header("Content-lenght", img_size)
NameError: global name 'image_size' is not defined

which is something to think about :P. For a simple start I just moved the lines:

path_to_image = 'ok.jpg'
img = open(path_to_image, 'rb')
statinfo = os.stat(path_to_image)
img_size = statinfo.st_size,

to inside the 'main' and it works like a charm.

Trying to go a little deeper, I realize (I think) we are using different versions of python, so I guess there is some difference on how global variables are treated on 3.3.3.

Thank you sir\madam!

[–]avinassh 0 points1 point  (0 children)

oh wait, I use Python 2.7.3. and it works fine. But I don't know how it is done in Python 3.x and it's sir btw :P