[Tutor] Writing scripts and apps for Internet consumption

Alan Gauld alan.gauld at btinternet.com
Sun Aug 1 02:12:53 CEST 2010


"Eric Hamiter" <ehamiter at gmail.com> wrote

> the most rudimentary level of the basics. What I keep reading is how 
> Python
> is most powerful on server side applications, in the cloud, so to 
> speak. The
> portability of Python is also widely evangelized.

I'm not sure I'd agree with that assertion. Python is a geeral purpose 
programming
language and is equally at home on  the desktop as on a server. Where 
it is
definitely not at home is inside a web client - that territory lies 
with Javascript
and Flash etc.

> Here's my problem with this so far-- I can write a basic script, 
> have it
> take in data, rearrange it, and spit it back out. Following along in 
> a book,
> I can write a basic GUI or game. It's all wine and roses on my 
> Windows
> laptop, where I have everything configured just right, with all of 
> the
> modules in place where they need to be.

Good thats as it should be.

> Moving this to a server or even another computer so far has been a 
> seemingly
> impossible task.

Moving desktop applications to a server is a non trivial task in anty 
language,
the paradigms are completely different and will usually require 
extensive
re-engineering of your designs. The most important aspect being that 
server
applications don't generally have a user interface - that goes on the 
client...

> There's a lot of documentation for CGI scripting (which is
> now frowned upon, with every page recommending looking into wsgi),

Yes although CGI is fine for very simple apps, but ay7thing more than 
trivial
really is better done on a web framework - but the frameworks all have 
a
steep learning curve. A bit like learning a new GUI framework - the 
principles
are all the same but the details are different..

> have been applications devoted to transforming scripts into Windows
> executables (py2exe, etc.) but it seems like this is much more 
> confusing
> than need be, and I can't get them to work regardless.

This is a fairly complex task and personally I prefer to just install 
Python
on the PC. After all thats what these packaging tools do under the 
covers,
you just get a copy of python for every application instead of a 
single
shared version... The tools make it as easy as they can but its never
going to be a trivial task.

> for solutions, choosing any terms like "web" or "server" point me to 
> massive
> framework solutions like Django or Pylons,

These are not massive although they are non trivial.
Massive is something like Zope or Plone...
But they do make web programming much much easier than basic CGI.
And the more complex the application the more useful they become.


> which seem extraordinarily complex for what I want.

Have you considered that what you want might be more complex than
you suspect? Web programming is more complex than desktop
programming and combines elements of GUI design, network programming
and server programming. To do it well requires a fauirly high level of 
skill - although
there are plenty folk out there not doing it well as the number of 
slow and
badly designed (from a users point of view) web sites testifies!

> Specific examples: I have a livewires/pygame GUI game I wrote along 
> with
> folowing the book "Python Programming for the Absolute Beginner" and 
> it
> works great on my laptop. I tried installing Python/pygame on a work
> computer and copying my scripts over, but it endlessly fails with 
> errors so
> obtuse I can't troubleshoot.

OK, providing you are just rubnning the game on the work PC in the 
same
way that you do on the laptop it should be fairly straightforward, 
assuming:
1) Are they the same versions of Python/GUI?
2) Arte they installed in the same place on the two machines?
3) Are the environment variables set to the same values?

If all of these is YES then we need some specific info about the 
errors.

> I'm not even sure if I have the correct modules
> installed here. Should they be under "Lib" or "libs" or "includes"?

If the modules are stangard(PyGame etc) then you shouldn't need to
worry about that the installers will have done that for you. You only
need to put your code in a place that Python can find it.

> to use py2exe fails because I can't figure out how to include 
> non-scripts in
> the final program, like .pngs or .jpgs.

Need to ask a py2exe persopn for that, but I'd get the non exe version
working first that should be esier!

> How would I even begin to put this on a server? I'm clueless.

If its a game you need to separate out the game logic, rules etc
from the GUI elelemts. The GUII elements then become a separate
client program that communicates with the server via some kind
of networking protocol. This is what I meant about re-engineering
your app for a server.

> Another program I am using on my laptop is a convenience script-- it 
> takes
> in a text list of groceries, and spits out a formatted list based on 
> aisle
> locations so I can get in and out of the grocery store faster. My 
> laptop is
> the only place I can use this. I've tried using multiple CGI 
> examples, and
> it always results in a "File Not Found" error. Not even sure how I 
> can debug
> it. I can have the server do a simple one-line of printing "Hello 
> World" but
> anything more complicated than that makes it implode.

Again you need to design it as a cliernt server app. The client 
captures
the user input, sends it to the server and when the server spits back
the data the client reads it and formats it for the user to see. In a 
web
context you probably need web pages for input and output. The input
one will have an HTML form with a submit button.

The server will need to read the data submitted by the form, perform
the translations and return the data. If you use raw CGI the server 
will
return the data as an HTML page. If you use a framework it will 
typically
be returned as a Python dictionary, depending on the framework You 
will
then define the output page to substitute the data into special 
markers
(based on a templating engine).

Python is a great tool for writing web server apps. But writing web 
server
apps, even in Python, even with a framework, is much harder than 
writing
desktop apps.

> The most frustrating thing is how flippantly experienced programmers 
> say to
> use Django for Python web apps because it's so simple to use.

Simpler than CGI for non trivial apps is what they mean.

> good half-day to just install it, and unless I'm writing a sample 
> code or if
> I want to clone a newspaper service, I have absolutely no idea how I 
> would
> use it efficiently.

You need to work through the extensive tutorial documentation.
Do not be misled, writing web apps is not trivial. Knowing Python is 
just
the start.

> I want to learn the basics before running off to learn a
> new framework.

So write a simple application using CGI.
Something like an adfdress book or library system.
A simple search/display/add/delete/edit type application.

That will give you enough to understand what a pain CGI is and
appreciate what a help the frameworks really are.

> In short, is there a "simple" method for putting python scripts onto 
> a
> server that I do not host myself?

No.
There are ways that are easier than others but server programming
is not simple. And converting desktop apps to server apps is non 
trivial.
Of course you can write apps that can be run on a desktop or a server
as needed but they have to be built for that from the begginning. Its
much easier to convert a server app to desktop use than the other
way round.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list