I come from php community and just started learning Python. I have to create server-side scripts that manipulate databases, files, and send emails.

Some of it I found hard to do in python, comparing to php, like sending emails and querying databases.

Where in php you have functions like mysql_query(), or email(), in python you have to write whole bunch of code. Recently I found Django, and my question is: is it a good framework for network-oriented scripts, instead of using it as a web-framework?

  • 5
    As someone who started out in PHP, stick with Python - I don't want to hurt any love you have for PHP - But you are going from a Ford Fiesta to a Bentley - you won't have to look back.
    – Ben DeMott
    Dec 1, 2011 at 8:46

3 Answers 3



This answer does not say which language is better, its sole purpose is to show Python code for things you said you needed from Django and tries to prove that in Python, as contrary to some other languages, you do not need frameworks to perform needed actions.

Python's "batteries included" vs. PHP

In PHP you often need a framework or separate script / library to perform some more advanced tasks.

In Python a lot of tools come bundled, thus the term "batteries included", referring to multiple standard modules you can use without need to download them etc.

Thus your approach from PHP may not work - Python itself is very powerful and does not need frameworks for most features.

Querying the database in Python vs. PHP

Actually in Python it is a lot easier to do such things as querying the database. For example querying the database looks like that in Python:

import MySQLdb as db

conn = db.connect(host='localhost', user='user', passwd='password', db='database')
cursor = conn.cursor()

cursor.execute('SELECT * FROM `some_table`')
result = cursor.fetchall()

where the first line (import statement) can involve other databases (such as sqlite3), as Python approach is to have the same interface for different RDBMSs. This means a lot of flexibility.

In PHP you may write the same thing similar to this:


$db = mysql_connect('localhost','user','password') or die("Database error"); 
mysql_select_db('database', $db); 

$query = 'SELECT * FROM `some_table`'; 
$result = mysql_query($query); 

while ($row = mysql_fetch_row($result)) {
    // do what you need with $row

which uses 4 MySQL-specific PHP functions. This code will not work for different RDBMs without replacing these functions. Of course, the better approach would be to use eg. PDO, but you said you are using mysql_query() function - my example just follows that.

Sending emails in Python

Here is an example from documentation page using SMTP:

import smtplib
from email.mime.text import MIMEText

msg = MIMEText('Hello, world!')
msg['Subject'] = 'This is an email title'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())

In PHP sending email may be very easy (and shorter, as in case of mail() PHP function), but considering some often used solutions for sending emails through SMTP in PHP, Python's approach may be considered a lot simplier.


Python is fun! :) If you need to query the database, it is a pleasure to do it in Python. If you want to send emails, Python has built-in module for it too. You do not need to use big, complex frameworks to accomplish these tasks.

  • Thanks, that's a great answer! I have a problem running this email script though, it says: socket.error: [Errno 61] Connection refused. Do you know what the problem might be? It seems like python didn't like s = smtplib.SMTP('localhost') part for some reason.
    – NoobDev4iPhone
    Nov 30, 2011 at 9:31
  • @NoobDev4iPhone: Error message says it all, the connection has been refused. Check the IP and port (if they are correct), check the firewall on both machines, client and server (if it blocks the connection, make it pass through), and if it fails, maybe replace localhost with actual IP address, as it may not be a localhost.
    – Tadeck
    Nov 30, 2011 at 9:41
  • I've retracted the downvote and deleted my comments, as after your update they were redundant. And Python is indeed fun!
    – yannis
    Dec 1, 2011 at 15:47
  • @YannisRizos: Thanks! Yes, it is. PHP also gains a lot with newer versions (5.3 was major improvement and I regret to say I saw too much hosting providers with default PHP version below that, but that is off-topic). I am going to remove my comments as they are pointless now. Good luck!
    – Tadeck
    Dec 1, 2011 at 16:46
  • I guess you also need to show that you will need to be iterating over result in the Querying The Database in Python example to be fair. Dec 1, 2011 at 21:19

No, there are many Python libraries you can use instead of using a giant web framework.

Have a look at: http://wiki.python.org/moin/DatabaseInterfaces to help you decide on a database interface; And a quick google query revealed: http://docs.python.org/library/smtplib.html as a possible solution to sending email.

Point being, a framework like Django is aimed at solving a specific problem, that being - easily creating a feature-rich website using Python. Not sending emails. :)

  • Thanks for the answer! I just found out about DatabaseInterfaces, which should work for me. But is there a way to send email without SMTP, with custom from-address, like you do in php? I want to send emails from localhost and can't find the solution to do so.
    – NoobDev4iPhone
    Nov 30, 2011 at 6:39
  • 1
    Looks like you're not the first to ask this question, a quick google search would have solved your problems. :) stackoverflow.com/questions/6485620/…
    – potato
    Nov 30, 2011 at 6:48

If you are still using mysql_* functions, your PHP skills are extremely outdated.

mysql_* are essentially deprecated since mysqli_* functions came to be, and even mysqli_* has long been surpassed by PDO. Also there's no email() function, but mail(). This may just be a typo in the question, if it is please ignore me.

Now as for the core of your question: (Almost) Everything that's available "out of the box" in PHP is also available in Python. You have to dig a little deeper into Python, before assuming a feature doesn't exist, your examples are pretty basic and a simple search on StackOverflow or Python's online documentation would have sufficed.

Although I'm primarily a PHP developer, I'm not advocating PHP over Python. Both are beautiful languages (in their own ways), with vibrant communities and healthy and stable ecosystems. And both are great for web development. You should choose the one that helps you be more productive, the one you find more fun to work with.

As for Django: It's a framework for web applications, based on the MVC pattern. It's a Python equivalent to PHP frameworks like Zend Framework, Kohana, CodeIgniter, Symfony and others.

You should use it when building Python applications that would require you to use an MVC framework if you were building them in PHP. So if your network-oriented PHP scripts don't use an MVC framework, Django probably won't be good fit either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.