Posts Tagged “Python

Simple Python Sendmail Script

Recently I found the need to have sendmail on one of my servers to send me email notifications, but didn’t want to have sendmail actually installed. So some python and smtplib made for an easy, ‘sendmail compliant’ alternative that I could use with services that interface with sendmail for email notifications. The python script is as follows:

#!/usr/bin/env python2
 
import sys, smtplib
 
SERVER = "smtp.gmail.com"
PORT = 465
USER = "email_address_or_username"
PASS = "email_password_here"
ADMINS = ("list_of_recipients@domain.com",)
 
if __name__ == "__main__":
        message_text = ""
        for line in sys.stdin.readlines():
                message_text += line
 
        server = smtplib.SMTP_SSL(SERVER, PORT)
        server.set_debuglevel(0)
        server.login(USER, PASS)
        server.sendmail(USER, ADMINS, message_text)
        server.quit()

(more…)

Filed under:
Tags: , , , ,

Integrating Uploadify with Django

29-07-2011 Alec Hussey 5 Comments

As multimedia becomes more and more prevalent these days, an interactive file uploader becomes extraordinarily useful. I ran into such a situation recently when developing a Django web application of my own. In this case I chose to use Uploadify because I’ve used it in the past, it integrates well with jQuery, and in my experience has been not only the easiest to work with but the least problematic.

Working with flash-based file upload systems is relatively easy with Django assuming that they send a standard POST request to the server and you understand the way Django chooses to handle file uploads. Django provides file upload handlers which may be modified to perform more sophisticated tasks during file uploads if you wish, but in this case we will stick with the built in upload handlers. When you upload a file to a Django view it takes the name of the upload field in your template and puts that name into the request.FILES dictionary where the value of that entry is an UploadedFile object or a subclass thereof. This UploadedFile object contains basic information about the file being uploaded including its file name as it was uploaded and its complete size. In addition, it gives you file-like access methods to the uploaded data which allow you, the programmer, to decide how the data gets stored.

(more…)

Filed under:
Tags: , , , , , ,

Integrating TinyMCE with Django

09-12-2009 Alec Hussey 9 Comments

Many people often want What You See Is What You Get (WYSIWYG) editors when creating content from within their web applications. This can be either in the Django admin control panel or for the end user. One of the most flexible and useful of the WYSIWYG editors currently available is TinyMCE due to its extensive plugin library and robust feature set. Integration with Django is relatively simple given that you extend the functionality of built in classes.

The first thing we will need to do is create a custom widget from forms.Textarea found in the django.forms module. We accomplish this by inheriting the Textarea class and overriding the constructor and render methods. Defining the relative path (on your web server) to the TInyMCE javascript source is also required here. So be aware that you will need to change that path to suite your environment. You may also want to change the content_css variable to include your sites’ main CSS file. The following is the source code for widgets.py which should be place in your projects root directory.

(more…)

Filed under:
Tags: , , , ,

Using the Rcon protocol with Python

26-11-2009 Alec Hussey 4 Comments

Within the PC gaming community, it is quite useful to be able to control or monitor your servers programmatically. The almost universal standard being Rcon or “Remote Connection.” The protocol is overall very simple, it only requires a standard UDP socket and that you send a packet header with the login credentials per each request you make.

We will start by importing the python socket module and creating a UDP connection to the server.

import socket
 
if __name__ == "__main__":
   sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
   # Connect to CoD4 server at 8.9.17.24:28960
   # Will also work for most other games in a similar fasion
   sock.connect(("8.9.17.24", 28960))

(more…)

Filed under:
Tags: , , , ,

Interfacing Banshee 1.0 with DBus and Python

10-06-2008 Alec Hussey 3 Comments

Ever since the DBUS API change (more like overhaul) during the development of Banshee 1.0 and developers haven’t yet started supporting it in their plugins so I decided that I would play around with it. From what I have seen, it seems that the perception is that DBus is hard and complicated but its actually really easy and makes things very simple. Essentially you use a DBus debugger (because in most cases, documentation for an applications’ DBus API is not available) like D-Feet to look up which interfaces, methods, properties, and signals are available to you. Then use them to do what you want.

(more…)

Filed under:
Tags: , , ,

Easily create IRC bots in Python with PyBotlib

09-05-2008 Alec Hussey 2 Comments

Back when I wrote ProxyBot, I was frustrated by the fact that there was a lack of maintained and documented third party libraries for the IRC client protocol. So I essentially wrote my own implementation of the IRC client protocol for use with the bots that I write. Well I wrote the library to take advantage of OOP (Object Oriented Programming for those who don’t know) which in turn made it really quick and easy for me to throw together a bot.

So this is a simple little tutorial on how to write a basic “Hello World” bot using PyBotlib. The library itself is only a single file so I have not bothered to repackage it or write setup scripts as you will only need import that single file within your application. You may get the latest source code for the library at any time here (google code/SVN) or here (blog). So here we go…

(more…)

Filed under:
Tags: , , , ,