News for hackers. Stuff that matters.
Re:I only know PHP. How do I write a Web application in Python? by zokier on Sunday May 06, 2012 @04:25AM (#3934527)
CGI scripts have much of PHPs simplicity. You can just drop in a executable file, and depending bit on server configuration, it will just run. With Python a basic hello world would look something like this:

    #!/usr/bin/python
    
    print "Content-type: text/plain"
    print ""
    print "Hello World!"
So 3 lines of "boilerplate", and then you can do anything you want. No extra packages to install or languages to learn. No frameworks necessary.

edit: just as an example of minimal hello world in WSGI:

    def application(environ, start_response):
        start_response('200 OK', [('Content-type', 'text/plain')])
        yield 'Hello World!'
also doesn't need any extra python packages or frameworks. but configuring web server to handle wsgi is bit more involved than cgi.
Re:I only know PHP. How do I write a Web application in Python? by raverbashing on Sunday May 06, 2012 @09:06AM (#3935018)
And therein lies the problem, in WSGI

99% of web sites are hosted in "shared hosting" services

Now compare copying the .php files with configuring WSGI

"No, but WSGI is easy blah blah blah" it doesn't beat copying files And no, settings WSGI is not easy

Also, if you depend on IIS for example, PHP is almost plug'n'play

WSGI on Windows is the definition of "bag of hurt". You can try fastcgi to run on windows, and then you find out flup needs socket.fromfd

The easiest way, if it's your machine, run gunicorn or similar and just make Apache redirect the traffic.

But in shared services it's still way too complicated.

Re:I only know PHP. How do I write a Web application in Python? by zokier on Sunday May 06, 2012 @02:15PM (#3935800)
FastCGI works fairly well in my experience on shared Linux servers (that's how I host my personal projects). And for smaller scripts where I don't bother setting up FCGI, I just use plain CGI, which is just copying files.
Re:I only know PHP. How do I write a Web application in Python? by raverbashing on Sunday May 06, 2012 @02:49PM (#3935912)
I think shared hosts have already enabled FastCGI for all php files

Something like this: http://test.fastcgi.com/drupal/node/5?q=node/10

At least the shared hosting I use (bluehost) has warned me they were enabling this. In 2007

Still, for most uses, CGI works fine.

Re:I only know PHP. How do I write a Web application in Python? by lukeholder on Sunday May 06, 2012 @02:42PM (#3935894)
Re:I only know PHP. How do I write a Web application in Python? by duskwuff on Sunday May 06, 2012 @04:05PM (#3936147)
That only affects PHP scripts running as CGI (using a certain specific configuration), not CGI scripts in general.
Re:I only know PHP. How do I write a Web application in Python? by mjschultz on Sunday May 06, 2012 @01:16PM (#3935611)
Let's look at the PHP version:

    Hello World!
One line. I didn't have to worry about Linux/Windows, knowing HTTP headers or the fact that I need two CRLFs after the last header[1]. I just wanted a page that says "Hello World!" and that's what I got.

Don't get me wrong, I would never choose to write a web app in PHP over python. But it is brain dead simple to go from nothing to something in a few keystrokes. Which is precisely what people just learning to write web apps want (right or wrong).

[1] I can't remember how long it took me to figure out that I needed the second CRLF after the "Content-Type: text/html" when I was learning perl (or even the fact that I needed to specify the Content-Type in the first place).

Re:I only know PHP. How do I write a Web application in Python? by zokier on Sunday May 06, 2012 @02:33PM (#3935856)
In the context of the parent comment I'd argue that the differences are insignificant. Xuzz argued that PHP is simpler because you don't need to know the ecosystem. Which I countered by showing a basic example of Python which doesn't depend on the ecosystem (like frameworks, package managers or version control).

Besides that, I don't think knowing HTTP headers or understanding significance of trailing newlines is required to write CGI scripts. You only need to know that you need those three 'magic' lines in the front of your scripts. Similarly like you need to know that in php you need <?php ... ?> around your script.

Re:I only know PHP. How do I write a Web application in Python? by mjschultz on Sunday May 06, 2012 @04:25PM (#3936215)
But I never needed to worry about those three lines in PHP, to a new user that is important. To you and I they come naturally.

PHP "is especially suited for Web development and can be embedded into HTML" [1]. Python "lets you work more quickly and integrate your systems more effectively" [2]. They can both do the task of the other, but the upstart time for a developer to handle the task of making a web app is smaller for PHP.

I've never taken the non-framework approach with python for anything significant, but how does it handle cookies, GET and POST fields, sessions, and file uploads? In PHP you'd have $_COOKIE, $_GET, $_POST, $_SESSION, and $_FILES as built-in global variables.

Are these differences still insignificant?

[1] http://php.net/ [2] http://python.org/

Re:I only know PHP. How do I write a Web application in Python? by eevee on Sunday May 06, 2012 @06:55PM (#3936683)
Python has a `cgi` module that does most of the superglobal stuff for you.

But, er. Does this really matter? Yes, you can get "hello world" in PHP with one line of not-PHP. You can also get "hello world" in Python + Flask with seven lines, and you'll get half a dozen extremely nice features for free that you can learn about as you go.

Arguing about what stack has the simplest way to build from total scratch is a sad race to the bottom. I'd rather be making the point that rich Web development can be easy, not that cobble-your-own-thing-together-with-duct-tape can be easy.

Re:I only know PHP. How do I write a Web application in Python? by mjschultz on Sunday May 06, 2012 @07:49PM (#3936873)
The first poster in this thread made the statement: "If I need a quick dynamic page on my server, it's much easier to do this in PHP than in Python or Ruby."

So yes, it really does matter in the context of this thread. On the whole I agree with your philosophy that frameworks are there for a reason: you get many features for almost nothing.

But sometimes you just don't need a framework, you need something quick-and-dirty that just works.

Re:I only know PHP. How do I write a Web application in Python? by eevee on Sunday May 06, 2012 @07:52PM (#3936882)
But even something quick and dirty is longer than "hello world". I've written a couple pretty dinky things with Flask, and the minuscule overhead of typing `import Flask` was definitely worth having e.g. a solid template language and XSS protection ready to go.
Re:I only know PHP. How do I write a Web application in Python? by jmgao on Monday May 07, 2012 @03:58AM (#3938115)
Yeah, PHP is great to start off with, it's simple and everything in the language is obvio- Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
Re:I only know PHP. How do I write a Web application in Python? by kragen on Monday May 07, 2012 @11:03AM (#3939257)
That's only hard to understand if you don't speak Hebrew. Anti-Semite! :)
Re:I only know PHP. How do I write a Web application in Python? by eevee on Sunday May 06, 2012 @06:58PM (#3936694)
CGI is easy, yeah, but it has a lot of the same disadvantages as mod_php: no routing, no shared state, slow execution, server misconfiguration can expose your code, possible code execution if you accept uploads, etc.

There are approaches that'll let you dump a WSGI app (with a known interface) anywhere and have the web server still run it. Gosh, someone ought to write about those.

Re:I only know PHP. How do I write a Web application in Python? by eli on Sunday May 06, 2012 @07:07AM (#3934802)
It's a good point that of course python web apps don't have to be complicated. But what you're describing is also not the same as PHP.
Re:I only know PHP. How do I write a Web application in Python? by drsintoma on Sunday May 06, 2012 @08:05AM (#3934896)
plus you can not just mix html and python code, which might not be a good practice, but is incredibly useful for something quick and dirty.