Posts Tagged ‘python’

PySync Redux

Monday, May 28th, 2007

After some comments by Patrick, I decided to play around with classes and came up with the latest iteration of my little sync script. I also added a rudimentary log file.

import socket, os

class Server:
    """ Server class used for checking and syncing """
    def __init__(self, srv):
        self.server = srv

    def is_up(self, port):
        """ Checks for server availability"""
        rmtsocket = socket.socket()
        rmtsocket.settimeout(0.25)
        try:
            rmtsocket.connect((self.server, port))
        except socket.error:
            return 0
        else:
            return 1

    def run_sync():
        """ Run Unison to perform the sync """
        try:
            status = os.system("unison -batch")
        except:
            print "Error running Unison... ", status

def write_log(entry):
    logfile.write(entry + "\n")

logfile = open("pysync.log", "a+")

server = Server("192.168.1.99")
if (server.is_up(22)):
    write_log("Server's up, syncing now")
    server.run_sync()
else:
    write_log("Server is down, no sync performed")

logfile.close()

Python First Steps: pysync.py

Sunday, May 27th, 2007

So, I finished my first little script. It does nothing but check to see if my desktop is up, and if it is, it will run unison to perform a sync. We all gotta start somewhere, right?

import socket
import os

rmtsock = socket.socket()
rmtsock.settimeout(0.25)

def checkServer():
    try:
        rmtsock.connect(("192.168.1.50", 22))

    except socket.error:
        return 1

status = checkServer()
if status == 1:
    print "Server is down"
else:
    rsyncrun = os.system("unison -batch")
    if (rsyncrun):
        return 1
    else:
        return 0

Fatherhood, Django and more

Tuesday, May 22nd, 2007

I’m a little behind in my blogging, as I missed out on last week’s installment of ‘The Geeky Dad’. I’ll be back this week with a new installment. I just need to figure out what I’m going to write about. You also might have noticed that the site has been changing a bit. I’m playing with themes again, trying to find one that suits me. I wanted a change, but it seems like most of the themes that I have been looking at just aren’t exactly what I’m after. So, just be warned that the site might change a bit more before it’s all said and done.

In more domesticated news, I made some lasagna over the weekend, following a recipe in one of the fatherhood books I’ve been reading. Other than the tomato sauce being more meat than tomato sauce, it turned out pretty well. I think with a bit of fine-tuning (and a proper lasagna pan), it will be even better the next go round. I’m trying to do all I can around the house for Beth while she’s pregnant. Granted, she’s a big girl and can still take care of things, but it’s my way of contributing to the pregnancy as a whole.

As far as geeking out goes, I’ve been playing around a bit with Python and now Django. Django is a Python-powered web framework, and while I should probably wait until I have a few regular python scripts under my belt, I couldn’t resist the urge to install it and play around with it. Surprisingly, it’s pretty straight-forward, and my understanding of Python seems to be a bit more solid than I thought. My main issue is using Python. Once I get to using it more often, I’m sure things will start to become second nature to me.

Oh, and one other thing. Bethany and I will be heading up to PA for Father’s day. I’m not sure what all we’ll be doing, but I would imagine we might have a bit of time to meet up with some of my PA friends at some point during the weekend. That should be pretty fun, I’m thinking. She gets to meet my family, I get to take her around my old stomping grounds, and I get a vacation from work. Sounds like the perfect little getaway to me.