Two ways to make python based webpages? - php

I wanted to try out python to create webpages instead of using php. However I came across that you need either mod_python or mod_wsgi installed to apache to make it play with python. If you now use pure, i'm not sure if it should be said pure, python code, not using any web frameworks like django. I found out that making a simple page looks differently in mod_python and in mod_wsgi.
How come?, the more I looked into python it just seemed to be a harder language to use to make webpages comparing it to php. Is there some good starting point to learn python webdevelopment?
Sorry if my question is blurry. I simply want some guidance to start out with python webdevelopment

Yes, making a webpage with python without using a web framework is harder than it is in php. This is by design, since it gives you a great deal more control over how your page interacts with the server, allowing you to build sites that scale well, among other benefits. WSGI is the modern way to interact with a server, but as you observed, it includes many of the nuts and bolts that PHP hides from the user.
If you're looking for a php-like experience for python, you might look at web.py or Flask. They are pretty minimalistic as far as frameworks go, and take care of interacting with the server but otherwise stay out of your way.
That said, you really should consider Django or another similar framework - they provide some really great benefits that help you get what would otherwise be painfully complex sites going quickly. They solve a slightly different problem and provide different solutions from the common PHP frameworks, so you should consider them even if you don't like frameworks in PHP.
If you want to do things in an even more php-like fashion, you could use CGI. It's definitely not a recommended solution, and won't teach you best practices moving forward, but it can get you started...
Really though, consider a framework. It's how most development in Python for the web is done, and you'll learn more useful skills if you develop using one.

mod_wsgi is better, because it's based on the WSGI specification, which defines the interface between web applications (or frameworks) and web servers. A WSGI app at its simplest is nothing more than a function that sends some HTTP headers via a callback and returns a string in response to information about an HTTP request. And since WSGI is implemented by many web servers, you aren't tied to Apache.
The closest you can get to pure frameworkless web development in Python is to write the WSGI app directly. This will really help you see the things that a framework like Django will obscure.
To make things easier, you might consider using Werkzeug, which is a utility library for WSGI. It has many components that are framework-like, but you can choose which ones you want and which ones you don't. For example, it has a really neat system for parsing and dispatching URLs. Werkzeug also has a simple command-line WSGI server which is probably better for development than Apache.

I'm replying to you with some advice, as someone who was in a very similar situation as you just a few months ago.
So you're using apache to host your website. That's cool. To make python play nice with apache, you're going to want to use mod_wsgi, for the reasons others have stated: seperation of concerns makes it better than cgi and mod_python is no longer being supported.
However, your impression that foregoing a framework will bring you closer to programming in "pure" python is a little bit off the mark. I shared the same opinion, and experimented with both Django and using only mod_wsgi. Let me share what I found.
Mod_wsgi is an implementation of the WSGI standard found in PEP 333. The distinction between the implementation and the standard is important. First, because it means that WSGI compliant applications will work across implementations. More importantly, it reveals something important about what WSGI is meant to do. That is, WSGI is intended a standard for writing frameworks. From the PEP:
simplicity of implementation for a framework author is not the same thing as ease of use for a web application author
and
The goal of WSGI is to facilitate easy interconnection of existing servers and applications or frameworks, not to create a new web framework.
I'm not saying that you shouldn't do something with wsgi, but you should expect to be writing a framework more than an application. If you're interested in writing a simple framework this tutorial is where I started.
However, if you're just looking to make a website, look into one of the frameworks that others have suggested. You'll still be writing python code, but the authors have worked hard to make the code you write closer connected producing websites than producing frameworks. I've personally used Django, and once it was up and running, it was rather painless to churn out interesting applications. Also, their documentation is very good, and they have a good tutorial here. That being said, Django is very fully featured, and if you're looking for something a little more minimalistic, I've heard good things about Flask, but there are lots of other options as well.

You can use ordinary CGI, which is really simple. Create a Python program that looks something like this:
#!/usr/bin/env python
import sys
sys.stdout.write("Content-type: text/html\r\n\r\n")
print("Hello <em>world</em>!")
Make this file executable (chmod +x) and put it in a directory you've configured for CGI files in your web server.
You will also find the standard Python cgi module very helpful.

If your goal is for making your python program web friendly then the answer is Cherrypy. It is a very flexible and simple framework that enables your python objects exposed in web. Check it out and it has a nice web server built-in that you don't need apache/mod_wsgi etc.,

Related

Web programming Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a amateur developer with a little experience in PHP.
I recently learnt Python, I've to admit that it is awesome as a programming language and its great libraries, but using that for web development seems to be a big pain in the ass. As far as what i read, I've to use CGI, mod_wsgi, etc. to deploy (http://docs.python.org/howto/webservers.html), Restart the server to see the changes (http://stackoverflow.com/questions/581038/python-web-programming)
My question is, Python being used so widely for web development, Why cant it be as simple as PHP?
ie i would love to write index.py:
print """
<html>
<head>
<title>Hello world</title>
</head>
<body>
Hello world
</body>
</html>"""
First to your question on why Python isn't as easy for web development as PHP: It hasn't been designed for it. PHP has had a lot of work towards making it a good language to write HTML pages in, including mod_php for apache. Python, while it is a very capable web language, it is also capable of far more than PHP. For example, the majority of Dropbox's software, both the desktop client and the server side code is Python. I use Python in my day job and have used it to integrate legacy C code into new Python code via Cython.
Moving on. There are a number of Python frameworks out there that can make your web development experience far less complicated. 2 of note that are widely used professionally and by hobbyists are Django and Pyramid (formerly Pylons). As a new user I'd recommend you start with Django, it's a great way to get started and can be used for large and complex sites (and more) as your experience grows.
You will find that Django and Pyramid include "development servers". These are web servers written in Python that are packaged with the frameworks to enable faster development. Using these development servers you can get up and running very quickly.
There are also pure Python web servers that are infact incredibly fast, robust and very production ready. To deploy a Pyramid application on Gunicorn, a very good Python server, only takes a couple of extra lines in your Pyramid configuration. Typically you would reverse proxy to these pure Python servers via Apache or Nginx.
If you want to go the Apache server route, you just need mod_wsgi. Serving a Python site with mod_python or as a CGI is a somewhat outdated way of doing it. WSGI defines a standard for Python to handle web requests so mod_wsgi enables apache to pass requests to your Python application in the expected format.
When it comes to deploying Python to a hosting provider, you have to look a little further than you would for PHP hosting, but there is a good list maintained on the official Python wiki. The 4 links at the bottom provided different lists to meet your requirements.
[If] Python being used so widely for web development, Why cant it be as simple as PHP? ie i would love to write index.py...
Python can be as simple as PHP, as per your example. The simplest way to make the above work is to use CGI (and call your file index.cgi rather than index.py, then your script will work just fine. Though you should really specify the content type first, like this:
print 'Content-type: text/html'
This would apply in PHP as much as Python. Python's CGI module will get you up and running very quickly.
As far as what i read, I've to use CGI, mod_wsgi, etc. to deploy
You would normally choose one or the other, and realistically you would most likely choose a framework which is based on the more modern WSGI and not worry about the low-level stuff. But if you don't want to, you don't need to bother with these low-level interfaces.
Restart the server to see the changes
Probably not. Having to restart the server is a limitation of the framework and deployment configuration, and is typically not required. If you use mod_python this may apply, but generally not (many frameworks support auto-reloading, for example).
Python has a great wealth of resources for web development. There are bare-bones systems (eg. CGI, mod_wsgi), lightweight systems (eg. CherryPy) and entire frameworks (eg. Zope). You can choose one that best suits your needs.
Being able to create a simple file and produce some output might help get something going quickly, but it is not really a sign of a rich, mature, featureful framework. Sure, you can start displaying something straight away, but what then? All of a sudden you will need to either import or write all the same sorts of functionality that web frameworks provide. For a non-trivial web app, you need non-trivial framework.
For example, something like Django will give you a persistence framework, auto-generated admin interface, a tempting language, URL dispatch system, and much more. You can focus on your app logic, and not worry about the lower level details. Django is a good place to start, as it has great documentation and the tutorial will get you going very quickly.
As others have said, google "python web framework". There are quite a few, they are all quite different. To get you started the fastest (hours), I'd look at bottle.py.
Python being used so widely for web development, Why cant it be as simple as PHP?
Because Hello World don't scale. Because Python ain't PHP. From wikipedia[1] [2]:
Python is a general-purpose, high-level programming language
PHP is a general-purpose server-side scripting language originally designed for Web development
Python is great, python is DRY friendly. Unfortunately, when it comes to web developement, DRY isn't enough. If you actually want to get things done (efficiently): don't reinvent the wheel. That was pretty well summarized here:
What seems the biggest shame to me is that everyone is currently rebuilding this stuff over and over again and rationalizing it as some sort of secret sauce competitive advantage when it’s really infrastructure – stuff that really should be standardized so you can actually get around to doing the new and interesting stuff.
So, use the right tool for the right job, if you go the python way:
use python and
use a web framework, Django is great (or another one)
reuse packages, browse them at django-packages
maybe use a "meta-framework" like Pinax to get started
use Twitter Bootstrap as an UI framework
use a PaaS like Heroku to deploy (or another one)
Yeah it's more complicated than a hello world but remember there ain't no such thing as a free lunch. You're an amateur developer, you're not a professional coder and a sysadmin and a database admin and a designer and a system architect, etc. Don't be too shy to stand on the shoulders of giants because now, it's free. Please, don't build nothing less than a modern web app.
Also, PHP isn't anymore popular as it was [1][2], cf. PHP's fractal bad design, PHP hammer, etc.
PHP was solely meant for web application development, to be more precise for building Personal Home Page(s) (thats where P, H and P came). Python on the other hand is a general purpose programming language. It was meant for a whole lot of things (and had to include all sorts of batteries for all sorts of tasks). So, please do not expect Python to behave the same way PHP does.
Stand alone python scripts will require quite a bit of efforts to run. I would suggest to pickup a web framework. You might want to learn django (https://www.djangoproject.com/) to begin with. You might also want to give Google App Engine a try. Besides heroku, dotcloud, nuagehq and other cloud hosts make python (and django) deployment very simple and easy.
Hope you'd enjoy developing web apps with Python! :)

Is Haxe really good for php-targetting server side development?

I have such a conception, to build a simple web application using some php/mysql hosting service. I'd also like to create a local version of that app. The local version should work similarly as the online one (i.d. using browser as the ui) and have the ability to communicate with the server. I want it to be as lightweight as possible.
Professionally, I'm a c#/.net programmer. I also have some experience with c/c++,javascript, python and java. I know very little about php, and honestly, I don't like the feeling coding in that language, hence, don't have much willingness to dig into it. .Net, python or java they're all too unwieldy, I can't force the users to install any of them. So I thought about haxe's multi-targeting. If I were to code the project in haxe (it seems pretty straight forward to learn, for some one with my background), I might use the same logic on both side of the server and the client, by targeting both php and neko.
As a novice, I have some doubts. I'm not sure if haxe for php alreadly mature enough for this kinda job. I know haxe is a cousin of ActionScript, it primarily targets AS, and because the author is also the inventor of nekoVM, therefore, these two targets should be fine. But uh, how about php? Is it really good for that as well? Any hints or suggestions? Is haxe's builtin libraries ok for a simple web app or should I use a framework? Does neko have a httpRequest functionality at all?
Much gratitude in advance.
The straight answer is that Haxe/PHP seems to really fit well for you. That because of you background. I used Haxe/PHP in several projects (it happens that I am also its author) and speed has never been an issue for me. If there are bottlenecks usually there are also ways to optimize those portions for better performances, and if in the end you application scales to billions of users you can always opt to switch to neko or a C++/cgi combo.
About the frameworks there are a few options too: Web "frameworks" for Haxe to deploy in a PHP environment?

What are the relative advantages of various Python/PHP web frameworks (particularly for my project)?

I'm deciding on a web framework for an upcoming project, and I'd appreciate any advice. We've decided to use jQuery for the JavaScript, and are heavily leaning toward Python or PHP (more Python) for our server-side logic. I'm especially interested in web2py because of its jQuery integration.
About our project
Our project is to develop a security console for a complex
cybersecurity system operating within an organization's internal
network.
This console will be largely server-driven, as messages come in from the network and must be pushed by the server to the user.
The user will also be able to initiate security actions, the implementation for which will likely be in C++.
The interface we've planned will be relatively rich, and I want to leverage jQuery's power as much as possible.
We have some control over the browser environment we'll be running in (e.g., we don't have to worry about clients with JavaScript disabled).
Our site is likely to have only a few, long-lived client connections.
We are looking for software components with permissive licenses, though we're using some copyleft components (I see that web2py is LGPL while Django is BSD, so +1 to Django)
We have about a month to create a functional demo of our system, of which this interface is a small (but visible) part.
About us
We are two developers with about 5 years of programming experience, but little web development experience. I have several years of Python experience and a summers' worth of experience messing around with PHP. My coworker has some Python experience and has never touched PHP.
I used Django once back in 2008, and was frustrated by the file and code structure, which I found highly unintuitive. Perhaps this structure is inherent to the MVC model (I've had similar experiences with Django and CakePHP since), and I just need to bite the bullet and memorize it.
My Question
Given the information above, what are the relative advantages of the various Python/PHP web frameworks for our project? As mentioned above, I'm especially interested in web2py because of its jQuery integration, though Django's dominance is (once again) hard to ignore.
Thank you very much for your time!
Before deciding on a framework, you should first decide if you want to commit to a language you are unfamiliar with.
You said you've both got minimal PHP experience, so you have to weigh up the advantages here; Will the pros for going PHP (if any) out weigh the amount of time the developers will need to spend to retrain?
(Although depending on your background experience, PHP should be very easy to pick up.)
If you frame it like that, PHP would have to have a pretty convincing offering to give you. From what I'm seeing, specifically Django vs web2py, they both seem very close in functionality - which is good, but doesn't provide the "you must use x!" scenario you may be after.
However, If you will be bringing more people in later and feel finding people to work with web2py will be difficult, it may tip it to PHP. You said your self, Django's popularity (and BSD license) is hard to ignore, and it should make it easier to find people for later expansion.
If it were me, in your shoes, I would go with web2py. Assuming the development team will continue to be Python focused for the foreseeable future.
Python vs PHP: Python
With python, you can always write wrappers for C code so you won't have to mess with starting other processes and passing args to them. That might be useful for your security functions.
Web2py will allow you to easily write a webservice for this too, to more easily integrate the C portions with the web-site infrastructure.
If you already prefer python, I would go with that. If you need to bring on web-developers later that are trained in PHP, teach them Python. It won't take long, and I'm sure they'll appreciate it in the long run. Plus, moving from a PHP MVC framework to web2py or even django would make things easier. I've used CodeIgniter for PHP and find that web2py was so much simpler and easy to understand.
Also as for the directory structure, django is not actually true MVC -- it's MTV (model, template, view).
I find web2py's organization a little more straight-forward. But yes, either way it can seem strange at first. I would say YES, you should bite the bullet and use MVC.
In web2py, the "view" is html markup with the ability to write raw python code. The controller extracts data from the model (database), attaches any needed files (css/js etc) and the model of course simply defines the structure of the data and allows you to access it in an OO way.
Lastly, I wouldn't tip my hat in favor of web2py just because of jQuery integration. It does use it, and a some of the built-in framework stuff (like response.flash/session.flash, the LOAD function that loads a page or data from another controller) rely on jQuery, but using it in another framework only means you have to write an include statement (e.g. ).
But, the way it allows/forces you to focus on development is what takes the cake for me.
I've been using Django as part of my work for a couple years now and truly enjoy it when I can make it work. Unfortunately, and maybe it's just me, but I end up spending hours working on configuration every time I start a new server, or try to make it work in a development IDE.
It's relatively simple to start a new project and start coding. But there are all sorts of little catches that keep things from working if you deviate from the norm. Things like if you want your django project to serve from a subdirectory like example.com/django. The information is out there to make it work. But it's hard to find.
I can't tell you if web2py has those same problems or not. I only just learned about it from your question. It does look slick and simple though. I can tell you that after the hassles of getting the applications to serve properly from whatever server configuration I'm using, django is way more fun to program with than plain php. PHP frameworks may differ.
From my understanding the project not usual web application, you must need event driven program, web server in python.
Django won't help you here.You should look into Flask, Flask has inbuilt console too.http://werkzeug.pocoo.org/, you might need to use twisted, gevent,Flask jquery.
I would recommend asking about web2py on its mailing list -- you'll get lots of help and advice there. Regarding the LGPL license, note that it only applies to the framework, not your application -- you can license your application however you like, including closed source/commercial.
It sounds like you'll need some server push functionality, but if you'll really only need a few long-lived connections at a time, you might not need to bother with solutions like Twisted or gevent. If necessary, though, web2py does include this (requires Tornado, and works using WebSockets, though can be adapted to use other methods).

Is there any harm in running PHP and Ruby on the same server?

I am an experienced PHP programmer, but I have been looking into Ruby lately, and it seems there are some neat features that I have been missing out on. I want to use PHP for all my important things, but then throw in a nice dash of Ruby on Rails when it makes things convenient (for, let's say, working with a Cassandra database, something not easily accomplished with PHP.)
Is there any harm in running PHP and Ruby simultaneously on one Apache setup? I would like to use some of the finer features of Rails, but I'm addicting to PHP and cannot give it up.
As long as your apps are well behaved there will be no problems.
Ruby, and/or Rails, just like PHP, could consume all available resources, but that's our own fault when it happens. Since you're experienced with PHP, I'm sure you're already aware of what sort of behaviors could bog down a server.
I say go for it.
Having written PHP code, along with using Ruby's Sinatra, Padrino and Rails for web front-ends, I'll also say "watchout". Ruby + Sinatra or Padrino + HAML can really seduce you to the Ruby-side.
These kinds of setups make the universe implode, so use with care. But on a serious note, you will have no trouble running them. As The Tin Man said, make sure you have enough resources. Even the best written applications with ROR will burn through resources. A poorly written PHP application will take down whole server. My rule of thumb: CPUs and RAM are cheap, but that doesn't mean your applications should be big and unruly.

php vs python django or something else for CMS module

We're looking to develop a CMS module for our website and I need some help in choosing the language/framework for this project. Basically we need to develop a "help' module like this one from ebay http://pages.ebay.com/help/index.html which will contain a lot of static pages with nice URLs for SEO.
The application must run fast using low computer resources.
We have been looking to use php on a custom made mvc framework but we received advice from other sources that py/django is the exactly language/framework that we need in terms of maintainability and development speed because it was developed for exactly this kind of projects so I need an expert advice on this matter with pro and cons for each choice.
It comes down to what your programmer is comfortable with. If you don't have a programmer, find one and ask him/her what she's comfortable working with. The task you're describing seems pretty simple. It can all be done with straight php, python, ruby. Having used PHP, Java and Python, I have a preference for the latter. But as I said, the task is so simple that you can do it without problem in most languages. I suspect that you'll find much more developers familiar with PHP, so you might want to look in that direction.
As far as frameworks go, there again, I don't see much that would require the raw power of an entire framework.
static pages: you need a cms, either custom built or something really lean and dead simple.
clean urls can be achieved with url rewriting (e.g. apache modrewrite directives).
Again, hire the right developer and trust his/her input. Don't go and tell him/her how to work if you don't know how it's done.
I would not say that django was specially developed for this kind of project. There are also great frameworks in PHP like the Zend Framework or symfony, among others.
In the end it comes down to what your production servers offers (but I guess you are free here) and with which programming language you feel most comfortable.
But it is true that Python's structure and style of language makes it fast to program with. (But it probably makes no difference if you are able to program PHP in your sleep ;))

Categories