Hi,

I'm trying to build a web server in Python, and here is what I have so far:

Code:
from http.server import HTTPServer, BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):
    def _writeheaders(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_HEAD(self):
        self._writeheaders()

    def do_GET(self):
        self._writeheaders()
        self.wfile.write("""<HTML>
<HEAD><TITLE>Sample title</TITLE></HEAD>
<BODY>sample text</BODY>
</HTML>""")

serveraddr = ('', 8080)
srvr = HTTPServer(serveraddr, RequestHandler)
srvr.serve_forever()

I do not get any errors, however when it runs the shell opens and nothing happens.
What changes to this code would I do to make it work?

Thanks.

I'm not that familiar with webservers in python but isn't it normal that the server just keeps running in the shell? You should probably use something else to see the servers output? Like a web browser or something?

I put a print statement at the end to make sure that the whole script was executed. It was not.

I put a print statement at the end to make sure that the whole script was executed. It was not.

Did you put a print statement after serve_forever() ? I'm pretty sure that forever indicates a super loop (ie, while True: )

Thanks. Apparently, the code finishes. Whoopse on my part, I usually don't miss those kinds of things. :)

So, my question is, now what? Once I have my server running, how do I access it and make it secure?

Any help on accessing it and making it more secure would be very helpful.

Thanks.

i think this line
serveraddr = ('', 8080)
binds the server to your localhost at port 8080
so you could try typing localhost:8080 at your webserver

What about accessing it remotely from a different computer?

What about accessing it remotely from a different computer?

Same as above but instead of localhost use the ip address of the computer where the server is running.

Thanks. When I run it and go to it via localhost in my browser (Safari) I get this in the Shell and I get a blank browser page:

localhost - - [11/Jun/2009 16:55:55] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54107)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 281, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 307, in process_request
    self.finish_request(request, client_address)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 320, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 614, in __init__
    self.handle()
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/http/server.py", line 363, in handle
    self.handle_one_request()
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/http/server.py", line 357, in handle_one_request
    method()
  File "/Users/brennonwilliams/Documents/basichttp.py", line 19, in do_GET
    </HTML>""")
  File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socket.py", line 219, in write
    return self._sock.send(b)
TypeError: send() argument 1 must be bytes or buffer, not str
----------------------------------------

Why don't you try sending something simpler... this is an example i found from googling:

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write("Hello World!")

That's strange. I just tried your code out and it worked with no problem. The only change I made was for the imports since I'm running it with py2.5

Here's what I ran:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class RequestHandler(BaseHTTPRequestHandler):
    def _writeheaders(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_HEAD(self):
        self._writeheaders()

    def do_GET(self):
        self._writeheaders()
        self.wfile.write("""<HTML>
<HEAD><TITLE>Sample title</TITLE></HEAD>
<BODY>sample text</BODY>
</HTML>""")

serveraddr = ('', 8080)
srvr = HTTPServer(serveraddr, RequestHandler)
srvr.serve_forever()

Then going to localhost:8080 in my browser resulted in a page with sample text

Dang, my computer is crazy. I always end up with weird errors on Python that on one else seems to get.

Thanks for the help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.