SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    python; just getting toes wet, pros/cons?

    only just found out that i could and how to execute python code on my server, and only just found out i can execute python code in the terminal app in os x; all you've got to do is type python, and you're away.

    anyway, just dipping my toes into python... what is python good and bad for? (i know the syntax is nice and clean). would you use it instead of php for example to do webpages with a bit of functionality? or not? also i have to put the python code file into the cgi-bin folder with a file extension of cgi then call it from browser to run it. if i wanted to use python like php, for webpages etc., how would the python file be called? i don't want urls xyz.com/cgi-bin/whatever.cgi. how's that usually handled? or do you not usually use python as a replacement for php, only use it in particular situations? if so, what kind of situations?

    thanks.

  2. #2
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Well, first of all, you should never have urls with .cgi no matter what your backend language.

    If you're running Apache you might want to check out mod_python which is basically a replacement for cgi (which is slow).

    The reason you would switch from PHP to Python might be if you liked Python more. Python is not a language that tries to be the fastest (pythonistas do not seek excessive optimisation), so you'll want other reasons like coding style, the extensive Python libraries (one of Python's strong points, like Perl's CPAN), or maybe because something else you're using is coded in Python (Gimp for example allows users to write their own Gimp scripts in Python).

    So far as I know, you can choose to run an entire site on Python just as with any other popular backend language, so it can replace PHP if you want.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    > Well, first of all, you should never have urls with .cgi no matter what your backend language.

    right, i'm just messing round at the moment though. just running a hello world script to see it work.

    > If you're running Apache you might want to check out mod_python which is basically a replacement for cgi (which is slow).

    would that appear in the phpinfo() output if it were installed/avaliable? i guess so. i'm using a shared hosting set up.


    i see right, thanks.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyway, just dipping my toes into python... what is python good and bad for?
    python is a general purpose programming language, which can be used to do anything. Because of it's clean syntax, great libraries, and the numerous python books (many recently published), I highly recommend python. Famously, google hired the top python gurus to work for them because google uses python extensively in house.

    would you use it instead of php for example to do webpages with a bit of functionality?
    Yes. In addition, there are also python 'frameworks' available for more complex websites.

    if i wanted to use python like php, for webpages etc., how would the python file be called?
    I think it depends on the server. For instance, with Apache set up for local development on my pc (which is something everyone should have set up), the url I use to run a python script that uses cgi (cgi is used to communicate with the server) is:

    http://localhost/cgi-bin/myprog.py

    Here is the script:

    Code Python:
    #!/usr/bin/env python
     
    import cgitb; cgitb.enable()
     
    print "Content-type: text/html"
    print
    print "<h1>Hello World</h1>"

    1) You need a 'shebang' line at the top of myprog.py.

    2) You need to change the permissions for the file myprog.py to give everyone execute privileges, e.g.

    $ chmod a+x myprog.py

    3) Put the file in the cgi-bin directory on the server, e.g. /Library/Apache2/cgi-bin

    i don't want urls xyz.com/cgi-bin/whatever.cgi. how's that usually handled?
    Servers, like Apache, allow you to map fake urls like:

    http://localhost/fake1/fake2

    to real urls like:

    http://localhost/cgi-bin/myprog.py

    In addition, python frameworks provide additional ways to map urls, an example is here:

    http://routes.groovie.org/manual.html#introduction

    If you're running Apache you might want to check out mod_python which is basically a replacement for cgi (which is slow).
    Setting up mod_python and figuring out how to call scripts is a bit of a pain, but if your host already has it setup, it is well worth learning. If you are going to use mod_python on your host, then you should bite the bullet and set it up for local development on your pc, too.

    Python is not a language that tries to be the fastest (pythonistas do not seek excessive optimisation)
    That isn't true. python is a language that has been developed with an eye on the speedometer. All the 'p' languages along with ruby, compete for users--and speed is a major selling point. Furthermore, python allows you to identify bottlenecks in your code, so that you can opt to rewrite those portions in an even speedier language like C\C++, and then call those functions from your python program. In addition, on discussion forums the efficiency of various solutions is always discussed.

    ruby is the language that plays down optimizations/speed--presumably because it can't compete with python's speed, so the rubyist's fall back motto is: if you need speed, you probably don't, but if you really do, then write the code in C--not ruby. There are plenty of speed tests posted around the internet, so you can decide for yourself where python stands.

    Because sitepoint puts no effort into any language except php, the best python discussion forum resides here:

    http://python-forum.org/pythonforum/index.php

  5. #5
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Maybe my impressions of Python not trying to be the fastest was based on old speed tests between Python and PHP in the cgi area (I read them years ago), and some once-popular retorts from the Python camp ("python should be good enough, otherwise look for bottlenecks in code"). I've heard of the "freezing" you can do with code but I don't know how it differs from something like mod_perl where all your modules get pre-compiled anyway.

    Ruby is of course known for being slow, even when it isn't. The Fail Whale is burnt into our minds too.

    So, maybe my impressions of Python are old. Initially the community was not trying to be the fastest. They wanted their code to be the cleanest, the easiest to work with, and focussed on the extensive libraries to make a ready-made Python solution to whatever problem you came across.

    If I hadn't already had Perl on the table as the planned First Backend Language, I might have chosen Python. It's a nice language. It's unfortunate that PHP dominates everything, but that's my opinion too.

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    > Maybe my impressions of Python not trying to be the fastest was based on old speed tests between Python and PHP in the cgi area (I read them years ago)

    cgi, regardless of which language is being run, is slow though itsn't it? it creates a new process for each run (possibly, could be wrong there). fast cgi which is what php makes use of i think avoids creating a new process each time, i think. could easily be talking total rubbish there.

    anyway, thanks both for all the info there. very useful.

    i found this which i haven't read yet about using python on a webserver: http://docs.python.org/howto/webservers.html

    thanks

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The python docs you linked don't recommend mod_python, so I would pay attention to that and disregard what I said above. The docs recommend something called WSGI if you are writing new programs.

  8. #8
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    cgi, regardless of which language is being run, is slow though itsn't it? it creates a new process for each run (possibly, could be wrong there)
    Yes, this is actually where Perl first got its reputation for being slow, since
    In The Beginning...
    Perl was CGI in many people's minds. And it had that very problem, everything being started up again every single time.

    fastCGI was one solution to that, but that got superceded by mod_perl, which is what PHP ended up doing too (mod_php) where your modules and stuff are pre-compiled... mod_perl was made specifically to work excellently with Apache so you could have just everything get going on startup and that was it... everything just kept running.

    The python docs you linked don't recommend mod_python, so I would pay attention to that and disregard what I said above. The docs recommend something called WSGI if you are writing new programs.
    Yeah the pages I found mentioning mod_python mentioned it being a bit hard to get going with Apache and also people who use 3rd-party hosters may have problems too. I did see something about "mod_wsgi" but hadn't read further into it. I don't know how it compares to the Python Server Pages that mod_python uses.

    Obviously if the OP has 3rd-party hosting then the hoster gets to determine a lot. Or if the server isn't Apache.

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    my server is apache.

    right so basically i'd have to ask the people/company who run my server if WSGI is possible. if not i'm stuck with putting things in cgi-bin and possibly url rewriting. or go to another hosting company who do offer WSGI.

    ok, thanks.

  10. #10
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    url writing is pretty much a separate thing, and it's recommended you do it anyway... supposedly, not letting people see what language you use behind your site is safer, or just a Smart Thing To Do. So even if you ran PHP, Perl, whatever... you're going to want to use mod_rewrite and there's a module for dealing with rewriting to a script who builds a page (scriptAlias), and those are just Apache, regardless of chosen back-end language.

    Anyway, hope you enjoy Python! Hook up with the Python community while you're at it. A language with a good community is nice.

  11. #11
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,301
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    > url writing is pretty much a separate thing

    right, but it takes on bit more of a prominant/necessary role if/while having to put pythonsourcecodefile.cgi in the cgi-bin folder.

    yup, i've always been a big fan of url rewriting to make urls as human as possible. not for any security reason but ease of use for the user. and more aesthetically pleasing (for me anyway)

    > Anyway, hope you enjoy Python!

    yup, thanks, i quite fancy it. know next to nothing about it at the moment apart from: it has a clean/simple syntax, it's object oriented, and can be used like php given a bit of preliminary set up. am in the process of starting a new project, kind of cms, so am thinking of python for that.

    looks like my hosting company can and will set up WSGI for me. not done and dusted yet but looks like it.

    great, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •