![]() |
PySite - Web Development Framework
It's not that I'm unhappy with CherryPy, it's just that when I decided to check out their source I was disappointed by the huge amount of code.
I didn't think it was necessary half the things they had there, so I decided I would try to make an optimum Web Development Framework for Python. It was surprisingly easy. I made a simple script that parses packets from port 80 and sends back the requested function. This is about two hours work. PySite makes it very easy to quickly create a dynamic website in Python. Just import PySite and apply it to a class. The return values of those functions will appear when you type 'localhost' in to your internet browser URL bar along with the appropriate function extention. If anyone would like to fill in my comments, fix bad code, or tackle some of the missing items. Go ahead. If you think this is good enough, you guys could help me publish it out there on the net. It actually works very well, it just needs all the missing stuff to reach v1.0. Best of all, it's only one file! It's even got some stuff I prefer over CherryPy. ;) See an example demo website below, along with PySite.py PySite.py :
import socketmy_website.py :
from PySite import PySite |
Part of the advantage, and perhaps a lot of the problem, with Python web development, is that there are a lot of web frameworks to choose from. You might be interested in googling on WSGI, which is an attempt to provide a standard to build these frameworks on top of.
Returning to your program, it's impressive, but I think you're reinventing the wheel a little. Why use raw sockets, when the standard Python library includes a BaseHTTPServer module for you to use? The advantages to using BaseHTTPServer are twofold: firstly, it's easier, and secondly you know that it adheres to HTTP specifications, which can be something of a minefield to do manually. :
from BaseHTTPServer import * |
Well I knew about the BaseHTTPServer but I figured "hand-rolled" functions are quite faster. Maybe not to as much of a degree here, but couldn't it make a difference?
And I still need to figure out the proper formatting for returning the html, because on FireFox you just see plain source. :( |
Quote:
One advantage in using BaseHTTPServer is that it's had a lot of use. People all over the world have coded with it, and thus there are likely to be fewer bugs in the implementation. Also, since BaseHTTPServer is based upon SocketServer.TCPServer, it also supports threading and forking mixins. Another, more obvious advantage, is that it does a lot of work for you. I can understand wanting to use your own systems. It wasn't so long ago that I shunned libraries and wanted to write my own implementations of all sorts of systems. It was only at University that discussions with my peers lead me to realise that a lot of the time, reinventing the wheel isn't necessary. Indeed, the time spent reimplementing a system could be spent inventing something entirely new, which may be much more interesting and satisfying. However, I think this is one of those things that a programmer has to come to realise on their own, and until then, my words won't hold much weight. Quote:
|
Well even if it is much more beneficial to use these libraries, I still feel like I'm learning a lot more doing it from scratch. I'm still quite new to programming. Hell, I didn't even understand sockets until yesterday. So I feel like this is the only way I will end up understanding these things clearly, and in the end that may be much more helpful.
|
I tried adding the content-type: text/html to the top of the socket return literal, but I guess that's not the proper way to format header info.
:
import socketIt's the commented part with content-type: text/html. |
IIRC, HTTP uses "\r\n"s instead of "\n"s.
|
Didn't help. Still shows up in the source. I think the header has to be sent as a completely different section or something. :S
|
The easiest way to check out what it is that your program's doing wrong is to send a HTTP GET request to your webserver, record the output, then do the same for a 'proper' webserver.
|
Okay, I sent an http request to my webserver with a raw socket, and got back
:
HTTP/1.1 200 OKSo then I sent that back to my webserver in a socket again, and the second time I got back the actual page (instead of the header). So I assume then that the header info should be returned before you get the actual page. I tried this method on PySite, first sending the client an identical header, then waiting for that header info to be sent back, then sending the site in return. So basically my server is like this: > Open socket >> Wait for HTTP Request >>> Send header info >> Wait to recieve header info back >> Send webpage > Close socket But the problem is the header info is never sent back... Here's my source: :
while 1:I've also tried starting a new socket object between the header request and the page request, but that didn't help. By the way, that double spacing is how these forums handle \r\n. |
| All times are GMT -5. The time now is 7:35 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC