endperform.org

Musings from /dev/random

Browsing Posts in programming

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.

This installment of Life with Emacs doesn’t have much in the way of opinions or what I’m using it for, instead I’ll just be posting the cheatsheet I’ve been keeping in hopes that it might help someone else out. As time goes on, I’ll keep adding to this. For now, I present to you my Emacs cheatsheet:

-- Buffers --
C-x C-f         Open existing (or new) file
C-x C-s         Save buffer
C-x C-w         Save buffer as...
C-x C-c         Exit Emacs

C-x b                   Switch to buffer. Type in the name.
C-x < - or ->    Cycle through buffers
C-x 1           Return to 1 window
C-x 2           Split window into two
C-x o           Switch windows

-- Moving / Editing --
C-a             Beginning of line
C-e             End of line
C-s             Search
M-x string      Find and replace
C-w             Cut
M-w             Copy
C-y             Paste (yank)
C-k             Cut text to end of line

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

I’m becoming a little annoyed with Emacs now. While it’s been pretty ok to use on a day-to-day basis for notes and other text chores, getting down and actually coding with it is quite, at least to me, annoying. My biggest problem is auto-indent. I can’t seem to shut this feature off to save my life, and it drives me insane. It seems to vary depending on the mode I’m in (php, python, etc), and the modes seem to override the settings in my .emacs file. While I do enjoy planner mode, the coding side of Emacs is basically annoying the heck out of me right now, and with what I’m working on, I’m thinking an IDE is going to suit me better in the long run. I may find the exact formula to change the behavior, but lately I’m not all that interested in that, rather than finding what’s going to be the most comfortable setup for me.

I’ve started messing around with Emacs in the past few days. It’s installed at work and here at home, but I haven’t done a whole lot of anything with it yet. I’ve been working on my .emacs configuration before I go too much further, setting and unsetting some things to get it to my liking. I found an interesting setup called org-planner mode which may end up in me being able to get organized at work *finally*. Time will tell, but I have to get some of the basics down first.

One thing I keep reading is that a lot of people are switching the Control and caps lock keys in their keybindings. It sounds like an interesting idea, and I can’t remember the last time I hit caps lock on purpose. I might go ahead and swap them out. So far, though, it doesn’t seem like it’s going to be too big a deal to learn and use.



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