Web Server code not working

Reply

Join Date: Jun 2009
Posts: 12
Reputation: sciguy77 is an unknown quantity at this point 
Solved Threads: 0
sciguy77 sciguy77 is offline Offline
Newbie Poster

Web Server code not working

 
0
  #1
Jun 8th, 2009
Hi,

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

  1. Code:
  2. from http.server import HTTPServer, BaseHTTPRequestHandler
  3.  
  4. class RequestHandler(BaseHTTPRequestHandler):
  5. def _writeheaders(self):
  6. self.send_response(200)
  7. self.send_header('Content-type', 'text/html')
  8. self.end_headers()
  9.  
  10. def do_HEAD(self):
  11. self._writeheaders()
  12.  
  13. def do_GET(self):
  14. self._writeheaders()
  15. self.wfile.write("""<HTML>
  16. <HEAD><TITLE>Sample title</TITLE></HEAD>
  17. <BODY>sample text</BODY>
  18. </HTML>""")
  19.  
  20. serveraddr = ('', 8080)
  21. srvr = HTTPServer(serveraddr, RequestHandler)
  22. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: Web Server code not working

 
0
  #2
Jun 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: sciguy77 is an unknown quantity at this point 
Solved Threads: 0
sciguy77 sciguy77 is offline Offline
Newbie Poster

Re: Web Server code not working

 
0
  #3
Jun 9th, 2009
I put a print statement at the end to make sure that the whole script was executed. It was not.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Web Server code not working

 
0
  #4
Jun 9th, 2009
Originally Posted by sciguy77 View Post
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: )
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: sciguy77 is an unknown quantity at this point 
Solved Threads: 0
sciguy77 sciguy77 is offline Offline
Newbie Poster

Re: Web Server code not working

 
0
  #5
Jun 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 39
Reputation: mathijs is an unknown quantity at this point 
Solved Threads: 2
mathijs's Avatar
mathijs mathijs is offline Offline
Light Poster

Re: Web Server code not working

 
0
  #6
Jun 10th, 2009
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
I could fill this thing with great stories and magical experiences.
But i guess i'm just to lazy ...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: sciguy77 is an unknown quantity at this point 
Solved Threads: 0
sciguy77 sciguy77 is offline Offline
Newbie Poster

Re: Web Server code not working

 
0
  #7
Jun 11th, 2009
What about accessing it remotely from a different computer?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Web Server code not working

 
0
  #8
Jun 11th, 2009
Originally Posted by sciguy77 View Post
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: sciguy77 is an unknown quantity at this point 
Solved Threads: 0
sciguy77 sciguy77 is offline Offline
Newbie Poster

Re: Web Server code not working

 
0
  #9
Jun 11th, 2009
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:
  1. localhost - - [11/Jun/2009 16:55:55] "GET / HTTP/1.1" 200 -
  2. ----------------------------------------
  3. Exception happened during processing of request from ('127.0.0.1', 54107)
  4. Traceback (most recent call last):
  5. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 281, in _handle_request_noblock
  6. self.process_request(request, client_address)
  7. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 307, in process_request
  8. self.finish_request(request, client_address)
  9. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 320, in finish_request
  10. self.RequestHandlerClass(request, client_address, self)
  11. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socketserver.py", line 614, in __init__
  12. self.handle()
  13. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/http/server.py", line 363, in handle
  14. self.handle_one_request()
  15. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/http/server.py", line 357, in handle_one_request
  16. method()
  17. File "/Users/brennonwilliams/Documents/basichttp.py", line 19, in do_GET
  18. </HTML>""")
  19. File "/Library/Frameworks/Python.framework/Versions/3.0/lib/python3.0/socket.py", line 219, in write
  20. return self._sock.send(b)
  21. TypeError: send() argument 1 must be bytes or buffer, not str
  22. ----------------------------------------
  23.  
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Web Server code not working

 
0
  #10
Jun 12th, 2009
Why don't you try sending something simpler... this is an example i found from googling:
  1. class Handler(BaseHTTPRequestHandler):
  2. def do_GET(self):
  3. self.send_response(200)
  4. self.send_header("Content-type", "text/plain")
  5. self.end_headers()
  6. self.wfile.write("Hello World!")
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC