endperform.org

Musings from /dev/random

Browsing Posts tagged python

I posted this bit of code some time ago as pysync.py, but I wanted to present it again in it’s latest form. Right now, all it does is print a message stating whether or not it has an SSH port open. This can be used as the basis of a script to do something based on an open SSH port. The port could also be changed to check for HTTP, telnet or what have you.

'''
Python code to check if SSH is open on a server
Updated: January 23, 2009

Brad Peters
brad (at) endperform (dot) org
'''

import socket
import os

'''
This code actually attempts to check the SSH port
'''
def checkServer():
    serverSocket = socket.socket()
    serverSocket.settimeout(0.25)
    try:
        serverSocket.connect(("192.168.1.20", 22))
    except socket.error:
        return 1

'''
If status is defined, we know the connection failed
'''
status = checkServer()
if status:
    print "Server is down"
else:
    print "I can ssh!"

This code is free, use it as you like. I just wanted to present it to show what I’ve been messing with. I have an idea for this at work. I haven’t messed with classes yet, but that’s next on my agenda.

I came up with something to parse some mbox-format email files out of boredom. The final step, coming later, is taking the data and making a line graph out of it. I’m sure it can be done a lot better, but at any rate, may I present the beginnings of mail-graph.py

import mailbox
import sys
import os

months = {'Jan': '01', 'Feb': '02', 'Mar': '03',
		  'Apr': '04', 'May': '05', 'Jun': '06',
          'Jul': '07', 'Aug': '08', 'Sep': '09',
		  'Oct': '10', 'Nov': '11', 'Dec': '12'}
emails = {}
total = 0

def accumulate_counts(email):
	for message in email:
		date = message['date']
		split_date = date.split()
		day = split_date[1]
		month = split_date[2]
		year = split_date[3]
		monthnum = months.get(month, 0)
		yearmonth = year + monthnum

		count = emails.get(yearmonth, 0)
		if (count == 0):
			emails[yearmonth] = 1
		else:
			emails[yearmonth] += 1

mbox_path = sys.argv[1]

for root, dirs, fileNames in os.walk(mbox_path):
	for fileName in fileNames:
		path = os.path.join(root, fileName)

		mbox = mailbox.mbox(path)
		accumulate_counts(mbox)

for m, e in emails.iteritems():
	print m, e
	total += e

print total

Comments are welcome, just be gentle. I know some variables are short, but I’ll fix that up on the next iteration

There’s a lot of talk lately on some of the blogs I read about code editors. I myself have been dealing with finding one that suits me (even if I don’t code all that often anymore), and so far, nothing seems to fit 100% yet. I’ve got a basic understanding of Vim, Emacs and Nano, which are all ok as far as command line utilities go. I think I fall into the IDE category when it comes to coding, so I’ve been playing around with jEdit, Eclipse and most recently OpenKomodo. OpenKomodo seems to work pretty well, and I really like the completion aspect of it, so I think I’m going to stick with it for coding for now. For configuration editing / remote work, I’ll stick with Vim.

My Django experiment is going relatively well. The database model is done, and now I’m at the point where I’m a bit stuck. I’m still learning my way around the framework, so I’m sure I’ll get it with time. What I’m currently dealing with right now is trying to figure out views. The way I understand it so far is that urls.py defines what happens when you go to a particular URL for the project, such as http://project/view/main_page, then in views.py there would be a main_page routine. Part of my hang up right now is the design aspect. In a nutshell, I’m trying to retrofit an old PHP application into Django while not altering it too much. I’m in no hurry to finish this, so I have time. As far as personal projects, I don’t really have any Django-related projects I could work on for personal things, so I’ve been thinking about some ideas I can do in just normal python. PyGame seems rather interesting, and of course I could always find some other project to get into as well. If you have any thoughts on projects looking for a semi-new Python guy, drop me a comment.

I figured that I’m going to start yet another series on my blog entitled “Adventures in Python”. I figure it will motivate me to play around with Python/Django while sharing my experiences with everyone. Keep in mind I’m new when it comes to Python, so my code may not be so pretty.

I’ve been playing off and on with Python for a while now, but never really did anything too awfully useful. Of late, I’ve been working on maintaining some internal web applications at work, which are written in PHP. After reading about Django and checking out some documentation, I’ve set out to rewrite at least one of those applications into Django. Thus far I don’t have much other than the database layout, but from what I can see I’ll have something rather functional in a short amount of time. I wish I had a non work-related project to work on, since I find it hard to work on the rewrite outside of working hours. Even during working hours my time is limited as I have other responsibilities to deal with, this being the lowest priority.

At any rate, learning more Python via Django seems to be going well so far. I am branching out, though, trying some new things and playing with some libraries. I’ll share some of my things when I finish.



© 2010 endperform.org - Powered by geekery, beer and a warped mind.