Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Web development with Python 3.1

Reply
Thread Tools

Re: Web development with Python 3.1

 
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> How can I get an array with all the GET variables in Python?
>> How can I get an array with all the POST variables in Python?
>> How can I get an array with all the COOKIE variables in Python?
>> How can I get the request URI path (everything after
>> http://[www.?]example.com/)?
>>
>> That's all I want: no templates and nothing between me and the HTML.
>> The HTTP headers I can output to stdout myself as well.

>
> $python
>>>help(cgi)
>>>help(cgitb)
>>>help(Cookie)
>>>help(urlparse)
>>>help(os.environ)

>
> GET variables:
> -------------------------------------------------------------------------
> import os,cgi
> if os.environ.has_key('QUERY_STRING'):
> Â* Â* Â* Â*getv = cgi.parse_qs(os.environ['QUERY_STRING'])
> Â* Â* Â* Â*print getv.keys()
>
> Other examples is very simple too...
>


Thanks, Mikhail. I now have a bit to google on.


--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
> 2009/10/28 Bruno Desthuilliers <>:
>> Dotan Cohen a écrit :
>>>> declarative mapping of urls to code
>>> Apache does this, unless I am misunderstanding you.

>> Using url rewriting ? Yes, fine. Then tell me how you implement "reverse"
>> url generation (like Django or Routes do).

>
> I have no idea what reverse url generation is.


It's a mean to avoid hard-coding urls in your application code - the url
is fully generated by the 'url mapping' system.

> I assume that the user
> will call http://example.com/path/to/script.py...llo&var2=world
>
> script.py would live in /home/user/site-name/public_html/path/to/
>


Now try to use apache url rewrites to use "nice urls" (like,
"/<section-name>/<sub-section-name>/<article-name>" instead of
"path/to/script-serving-articles.wtf?id=42"). Good luck.

>
>>>> of code to templates
>>> Those who code in HTML don't need this.

>> ???
>>

>
> I would prefer to output everything from <html> to </html> with print
> statements. I don't want some framework wrapping my output in tags, I
> want the tags to be part of the output.


You're confusing the framework and the templating system. The framework
by itself won't wrap your "output" in anything, nor even forces you to
"output" HTML. Also FWIW, it's HTTP, so we don't have "outputs", we have
HTTP responses.

>
>>> In any case it's not hard to
>>> call a function in a class that writes the HTML before the content,
>>> then write the content, then call another function that writes the
>>> HTML after the content.

>> Hmmm, yummy ! And SO maintainable...
>>

>
> Yes, why not?


Let's say I want to reuse your app with a different presentation. Do I
have to fork the whole application and send my HTML coder dig into your
applicative code just to rewrite the HTML parts ?

>
>>> This is how my sites are run, though in PHP
>>> instead of Python. No prepackaged templates.

>> PHP *is* a template language.


(snip)

>>> I would really like to know what else. So far, I am not convinced that
>>> a framework offers anything that is not already easily accomplished in
>>> Python.

>> Given that we're talking about Python frameworks, it seems obvious that what
>> they do can be accomplished in Python. Now the question is how much you like
>> to write the same boring and error-prone boilerplate code project after
>> project...
>>

>
> Like I said before, I don't want to have to maintain the functions
> that turn the HTTP environment into Python variables,


"HTTP environment" ???

Oh, you mean HTTP requests etc... Well, the cgi module already provides
some basic support here.

> or the code that
> manages database connections. Functions that escape data to be sent to
> MySQL (to prevent sql injection) would be nice.


Any DB-API compliant module already provide this - granted you use it
correctly.

> Other than that, it's
> all on me.


Your choice...
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>>> While I know that to be true in the general sense, from what I've
>>> looked at Django and other frameworks it seems that the web frameworks
>>> push the coder to use templates, not letting him near the HTML.

>> Well... there must be a reason, for sure... No ?
>>

>
> Ö¹Yes, but I don't like it.


Why so ? What's your problem with templating systems, exactly ?

>
>>> Django and the other frameworks seem to
>>> force the user to use templates.

>> Not at all. Nothing prevents you from shooting yourself in the foot and
>> generating HTML "by hand" in your view functions (or request handler methods
>> etc, depending on the framework).

>
> How is this done in Django, then?


def index(request):
unmaintanable_html = """
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Embedded HTML is a PITA</h1>
<p>but some like pains...</p>
</body>
</html>
"""
return HttpResponse(unmaintanable_html)


>
>> This is just plain stupid wrt/
>> readability, maintainability, reuse and whatnot, but hey, if you want to
>> waste your time, please do. As far as i'm concerned, I'm very happy to let
>> the HTML coder write the HTML part and the application programmer write the
>> applicative part - even when I end up wearing both caps.

>
> I've done stupider things.


Your choice, once again...

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>>> I should probably expand on this:
>>>
>>> How can I get an array with all the GET variables in Python?
>>> How can I get an array with all the POST variables in Python?
>>> How can I get an array with all the COOKIE variables in Python?
>>> How can I get the request URI path (everything after
>>> http://[www.?]example.com/)?
>>>
>>> That's all I want: no templates and nothing between me and the HTML.
>>> The HTTP headers I can output to stdout myself as well.

>> Again: if you insist on doing everything yourself - then of course any
>> library or framework isn't for you.
>>

>
> I insist on handling the HTML myself.


I just don't get how embedding HTML in applicative code - a well-known
antipattern FWIW - relates to "handling the HTML yourself". Can you
*please* explain how a templating system prevents you from "handling the
HTML" yourself.

>> I think you underestimate the task it is to make a webapplication good.

>
> Probably, but that would not depend on the scripting language. I make
> bad webapplications in PHP too!


Lol !

>
>> And
>> even if not, what you will do is ... code your own webframework.

>
> That is why I am looking for a class that handles the backend stuff,
> but lets _me_ handle the HTML.


For God's sake : *why on earth do you believe that using a templating
system will make you loose _any_ control on the HTML code ???*


>> And at least pylons/TG2 lets you return whatever you want instead, as a
>> string. Not via "print" though - which is simply only for CGI, and no other
>> means (e.g. mod_wsgi) of python-web-programming.
>>

>
> I am trying to follow you here. What is "return whatever you want"?
> Return HTML to stdout to be sent to the browser?


please leave stdout out of here - it's HTTP, so what we have are HTTP
requests and HTTP responses - not stdin nor stdout. Using these streams
as a mean of communication between the web server and the applicative
code is just plain old low-level GCI stuff. Your application code
shouldn't have any knowledge of this implementation detail - it should
receive (a suitable representation of) an HTTP request, and generate (a
suitable representation of) an HTTP response.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen wrote:

>>> I should probably expand on this:
>>>
>>> How can I get an array with all the GET variables in Python?
>>> How can I get an array with all the POST variables in Python?
>>> How can I get an array with all the COOKIE variables in Python?
>>> How can I get the request URI path (everything after
>>> http://[www.?]example.com/)?
>>>
>>> That's all I want: no templates and nothing between me and the HTML.
>>> The HTTP headers I can output to stdout myself as well.

>>
>> Again: if you insist on doing everything yourself - then of course any
>> library or framework isn't for you.
>>

>
> I insist on handling the HTML myself. As for converting the request
> variables into Python variables, if a class/framework makes that
> easier then I would gladly use it. My question was serious. How can I
> do those things?


Using a framework? In Pylons/TG2, my code looks like this:

def some_form_action(self, name, email, password):

....

so HTTP-variables get parsed, validated, converted, and *then* passed to my
actual code.

>> But then do you deal with headers correctly?

>
> Yes, so far as I know. This is actually simpler than the HTML, just be
> careful not to output two newline characters in sequence (thereby
> ending the header).


Oh my god. If two newlines are all you think that there is to properly
writing HTTP-headers, no wonder *you* wonder what to use a webframework
for.

http://www.w3.org/Protocols/rfc2616/...c5.html#sec5.3
http://www.w3.org/Protocols/rfc2616/...c6.html#sec6.2

That's just an *overwiew* of what different kinds of headers there are, all
of which might require specific rules to parse or generate.

>
>> Do you respect character
>> encodings?

>
> Yes! UTF-8 from database to scripting language to HTTP request.


And if the browser sends them in different encoding? Frameworks make you
deals with unicode only. Decoding/Encoding, setting the necessary

>> Form-encodings?

>
> Yes, UTF-8 the in the other direction. However, as form data can be
> spoofed, I would like a function that checks it. Does Python have such
> a function? What class does?


Frameworks do check for HTTP-headers that contain the encoding, and thus
convert incoming data to unicode.

>
>
>> Is your generated HTML valid?

>
> Naturally, even though this is not a Python issue.


"Naturally" with print-statements?

I use genshi templating, which is XML-based, so I can't even write invalid
HTML for a great deal of cases.

>
>
>> Are
>> timestamp-formats generated according to RFCs for your cookies?

>
> Yes, this is not a problem. Is there some gothcha that I am unaware of?


The thing is that using a framework, I just pass a datetime-object. Others
have done it right for me translating that to the needed format.

>
>
>> Do you parse
>> content negotiation headers?
>>

>
> No. I hate sites that do that. If the page is available in another
> language, their is a link in the corner.


Again, you don't seem to really know much about webapps. Content-negotiation
isn't about languages, it's about content-types. Such as serving HTML or
JSON from the same URL. But let me guess, you hate applications that do
that?

>
>
>> I think you underestimate the task it is to make a webapplication good.

>
> Probably, but that would not depend on the scripting language. I make
> bad webapplications in PHP too!


I don't have the *slightest* doubt about that. In fact, I'm pretty sure you
can make pretty bad webapplications in pretty much everything you touch.
Good luck with that.

Diez
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> I have no idea what reverse url generation is.
>
> It's a mean to avoid hard-coding urls in your application code - the url is
> fully generated by the 'url mapping' system.
>


I don't need that, see below.


>> I assume that the user
>> will call http://example.com/path/to/script.py...llo&var2=world
>>
>> script.py would live in /home/user/site-name/public_html/path/to/
>>

>
> Now try to use apache url rewrites to use "nice urls" (like,
> "/<section-name>/<sub-section-name>/<article-name>" instead of
> "path/to/script-serving-articles.wtf?id=42"). Good luck.
>


Actually, currently in the example url
http://example.com/path/to/script.py...llo&var2=world the script
is /home/user/site-name/public_html/path/ (with no filename extension)
and the script.py is actually page.html which is an arbitrary,
descriptive string and not part of the script filename. See the pages
on http://dotancohen.com for examples.


>> I would prefer to output everything from <html> to </html> with print
>> statements. I don't want some framework wrapping my output in tags, I
>> want the tags to be part of the output.

>
> You're confusing the framework and the templating system. The framework by
> itself won't wrap your "output" in anything, nor even forces you to "output"
> HTML.


This is what I was hoping to hear. I don't remember the details, but I
had a hard time outputting the HTML myself in Django. I will play with
it again soon if I get the chance.


> Also FWIW, it's HTTP, so we don't have "outputs", we have HTTP
> responses.
>


Clearly. I was referring to stdout being send to the browser as the
http response. I think I mentioned that, but I apologize for being
unclear.

>> Yes, why not?

>
> Let's say I want to reuse your app with a different presentation. Do I have
> to fork the whole application and send my HTML coder dig into your
> applicative code just to rewrite the HTML parts ?
>


No, just replace two files. In that manner it is templating. Again, an
example from the current PHP code:

<?php
$variables_for_header="blah";
include "/path/to/header.inc";

// content here

include "/path/to/footer.inc";
?>


>> Like I said before, I don't want to have to maintain the functions
>> that turn the HTTP environment into Python variables,

>
> "HTTP environment" ???
>
> Oh, you mean HTTP requests etc... Well, the cgi module already provides some
> basic support here.
>


Does it? I will look into that. I assume that basic support means
making the cookie, GET and POST variables easily accessible. I google,
but cannot find any exapmles of this online.


>> or the code that
>> manages database connections. Functions that escape data to be sent to
>> MySQL (to prevent sql injection) would be nice.

>
> Any DB-API compliant module already provide this - granted you use it
> correctly.
>


I know. This is will use correctly, I promise!


>> Other than that, it's
>> all on me.

>
> Your choice...
>


Hey! Since when am I not under attack?

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>>>> While I know that to be true in the general sense, from what I've
>>>> looked at Django and other frameworks it seems that the web frameworks
>>>> push the coder to use templates, not letting him near the HTML.
>>>
>>> Well... there must be a reason, for sure... No ?
>>>

>>
>> Ö¹Yes, but I don't like it.

>
> Why so ? What's your problem with templating systems, exactly ?
>


They don't give me the control over the HTML that I would like. I'm
probably stuck in 1998 when they didn't exist, but I like to write the
HTML myself.


>>>> Django and the other frameworks seem to
>>>> force the user to use templates.
>>>
>>> Not at all. Nothing prevents you from shooting yourself in the foot and
>>> generating HTML "by hand" in your view functions (or request handler
>>> methods
>>> etc, depending on the framework).

>>
>> How is this done in Django, then?

>
> def index(request):
> Â* Â*unmaintanable_html = """
> <html>
> Â*<head>
> Â* Â*<title>Index</title>
> Â*</head>
> Â*<body>
> Â* Â*<h1>Embedded HTML is a PITA</h1>
> Â* Â*<p>but some like pains...</p>
> Â*</body>
> </html>
> """
> Â* Â*return HttpResponse(unmaintanable_html)
>


And if I need to add a variable or three in there? Static HTML I can
do without Python.

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> I insist on handling the HTML myself.
>
> I just don't get how embedding HTML in applicative code - a well-known
> antipattern FWIW - relates to "handling the HTML yourself". Can you *please*
> explain how a templating system prevents you from "handling the HTML"
> yourself.
>


>From the little that I played with Django, it looked like I would (for

the sake of argument I am just using the term output, but the
intention should be clear) output "Hello, world!" and Django would
mangle that into <html><body><p>Hello, world!</p></body></html>. It
did not look like I had any control over the tags at all.


>>> And
>>> even if not, what you will do is ... code your own webframework.

>>
>> That is why I am looking for a class that handles the backend stuff,
>> but lets _me_ handle the HTML.

>
> For God's sake : *why on earth do you believe that using a templating system
> will make you loose _any_ control on the HTML code ???*
>


Because that's what I've seen of them.


>>> And at least pylons/TG2 lets you return whatever you want instead, as a
>>> string. Not via "print" though - which is simply only for CGI, and no
>>> other
>>> means (e.g. mod_wsgi) of python-web-programming.
>>>

>>
>> I am trying to follow you here. What is "return whatever you want"?
>> Return HTML to stdout to be sent to the browser?

>
> please leave stdout out of here - it's HTTP, so what we have are HTTP
> requests and HTTP responses - not stdin nor stdout. Using these streams as a
> mean of communication between the web server and the applicative code is
> just plain old low-level GCI stuff. Your application code shouldn't have any
> knowledge of this implementation detail - it should receive (a suitable
> representation of) an HTTP request, and generate (a suitable representation
> of) an HTTP response.


Can you provide a code example of printing the variable "torvalds" in
the GET request?
http://example.com/page.py?torvalds=tux
Should return this:
<html><body><p>tux</p></body></html>

And something similar for a POST request?

I hate to ask for code, but this conversation would be easier that way. Thanks.

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen wrote:

>>> I insist on handling the HTML myself.

>>
>> I just don't get how embedding HTML in applicative code - a well-known
>> antipattern FWIW - relates to "handling the HTML yourself". Can you
>> *please* explain how a templating system prevents you from "handling the
>> HTML" yourself.
>>

>
>>From the little that I played with Django, it looked like I would (for

> the sake of argument I am just using the term output, but the
> intention should be clear) output "Hello, world!" and Django would
> mangle that into <html><body><p>Hello, world!</p></body></html>. It
> did not look like I had any control over the tags at all.
>
>
>>>> And
>>>> even if not, what you will do is ... code your own webframework.
>>>
>>> That is why I am looking for a class that handles the backend stuff,
>>> but lets _me_ handle the HTML.

>>
>> For God's sake : *why on earth do you believe that using a templating
>> system will make you loose _any_ control on the HTML code ???*
>>

>
> Because that's what I've seen of them.
>
>
>>>> And at least pylons/TG2 lets you return whatever you want instead, as a
>>>> string. Not via "print" though - which is simply only for CGI, and no
>>>> other
>>>> means (e.g. mod_wsgi) of python-web-programming.
>>>>
>>>
>>> I am trying to follow you here. What is "return whatever you want"?
>>> Return HTML to stdout to be sent to the browser?

>>
>> please leave stdout out of here - it's HTTP, so what we have are HTTP
>> requests and HTTP responses - not stdin nor stdout. Using these streams
>> as a mean of communication between the web server and the applicative
>> code is just plain old low-level GCI stuff. Your application code
>> shouldn't have any knowledge of this implementation detail - it should
>> receive (a suitable representation of) an HTTP request, and generate (a
>> suitable representation of) an HTTP response.

>
> Can you provide a code example of printing the variable "torvalds" in
> the GET request?
> http://example.com/page.py?torvalds=tux
> Should return this:
> <html><body><p>tux</p></body></html>
>
> And something similar for a POST request?
>
> I hate to ask for code, but this conversation would be easier that way.
> Thanks.


I've already given you that for TG:

class RootController(BaseController):

@expose()
def page(self, torwalds=None):
return "<html><body><p>%s</p></body></html>" % (torwalds if torwalds
else ""


Of course nobody would do it that way. Instead, you'd define a template

<html
xmlnsy='http://genshi.edgewall.org/'><body><p>${torvalds}</p></body></html>


And then your code becomes:



@expose("your.template.path")
def page(self, torwalds=None):
return dict(torwalds=torwalds)


Separation of code from HTML-code. Which most people agree is a good thing.
And no worries about having to make torwalds a string.

Now let's say you want torwalds to be not-empty, and a number:

@expose("your.template.path")
@validate(dict(torwalds=Int(not_empty=True)),
error_handler=some_error_handler_controller_action )
def page(self, torwalds=None):
return dict(torwalds=torwalds)



Diez
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> I insist on handling the HTML myself. As for converting the request
>> variables into Python variables, if a class/framework makes that
>> easier then I would gladly use it. My question was serious. How can I
>> do those things?

>
> Using a framework? In Pylons/TG2, my code looks like this:
>
> def some_form_action(self, name, email, password):
>
> Â* ....
>
> so HTTP-variables get parsed, validated, converted, and *then* passed to my
> actual code.
>


I know how to call a function, but how do I use the variables? How do
I respond (I am avoiding the term output) with the value of the GET
variable "torvalds", for example? And similarly for a POST variable?


>>> But then do you deal with headers correctly?

>>
>> Yes, so far as I know. This is actually simpler than the HTML, just be
>> careful not to output two newline characters in sequence (thereby
>> ending the header).

>
> Oh my god. If two newlines are all you think that there is to properly
> writing HTTP-headers, no wonder *you* wonder what to use a webframework
> for.
>


I was being brief. I don't pretend to know the whole protocol by
heart, but I get by that far.


>>> Do you respect character
>>> encodings?

>>
>> Yes! UTF-8 from database to scripting language to HTTP request.

>
> And if the browser sends them in different encoding? Frameworks make you
> deals with unicode only. Decoding/Encoding, setting the necessary
>


That's what I wrote below:


>>> Form-encodings?

>>
>> Yes, UTF-8 the in the other direction. However, as form data can be
>> spoofed, I would like a function that checks it. Does Python have such
>> a function? What class does?

>
> Frameworks do check for HTTP-headers that contain the encoding, and thus
> convert incoming data to unicode.
>


That is useful, thanks.

>>
>>
>>> Is your generated HTML valid?

>>
>> Naturally, even though this is not a Python issue.

>
> "Naturally" with print-statements?
>


"Carefully" with print statements.


>>> Do you parse
>>> content negotiation headers?
>>>

>>
>> No. I hate sites that do that. If the page is available in another
>> language, their is a link in the corner.

>
> Again, you don't seem to really know much about webapps. Content-negotiation
> isn't about languages, it's about content-types. Such as serving HTML or
> JSON from the same URL. But let me guess, you hate applications that do
> that?
>


I used to serve WML-specific "decks" to known mobile UA's, but I've stopped..


> I don't have the *slightest* doubt about that. In fact, I'm pretty sure you
> can make pretty bad webapplications in pretty much everything you touch.
> Good luck with that.
>


I am a mechanical engineering student, not CS. I am doing this for my
own use and enjoyment. I don't need luck with that, I don't have any
customers. If someone were to come along asking me to code for them, I
would send them to you!


--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Application development vs windows client development cabernet123@hotmail.com ASP .Net 0 11-17-2005 12:09 AM
Lisp development with macros faster than Python development?.. seberino@spawar.navy.mil Python 37 07-11-2005 02:10 PM
Web Clients, the role of ASP.NET and the Future of Web Development and Web Standards Guadala Harry ASP .Net 9 11-06-2004 03:05 AM
development environment architecture for ASP.NET development team Akhlaq Khan ASP .Net 4 09-27-2004 01:33 PM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments