For those of you who don’t know Python, Python is a relatively new computer language that was quickly adopted by software giants such as Google, Microsoft, Red-Hat. Since Python has a very clear (almost algorithmic) syntax, anyone can learn it quickly, and if you have a little background in computer programing, it’s even easier. Because of those reasons, M.I.T had chosen Python in their introduction to computer programming.
If you’d like to get your hands a bit dirty, IronPython has a great tutorial with some hands on, highly recommended.
Since most of my work around computers involves Python for the past two years or so, I’ve decided to open a new section called the Python Challenge.
This week I was presented with a cool challenge: find a way to make your computer screen lock while you’re away from the computer.
My initial thought, Bluetooth! It has a short range, and Microsoft provides a nice interface to the Bluetooth stack. The only “disadvantage” is that you have to initially pair the device using Windows. This actually works for my advantage since it prevents possible malicious use.
The code below works only for locking the screen while away, and as a security precaution I’ve chosen not to include the section where the computer immediately logs on while you’re close to the computer.
Let’s get busy
1: #!/usr/bin/env python2:3: # Importing modules
4: from bluetooth import *5: import time6: import subprocess7:8: print "\nPlease wait while discovering devices...\n"9:10: # Discovering devices11: devices = discover_devices(lookup_names=True)12: counter = 013: for i,j in devices:14: counter+=115: print "%d) %s - %s" % (counter,i,j)16:17: device_num = raw_input("\n\nPlease select a device: ")18: selected_device =str(devices[int(device_num) - 1][0])19: print "Please wait, pairing with %s" % i20:21: # Create the client socket22: client_socket=BluetoothSocket( RFCOMM )23:24: # Attempt to pair25: try:26: client_socket.connect((selected_device, 3))27:28: except IOError:29: print "Bluetooth error, check your settings"30: raise SystemExit31:32: except KeyboardInterrupt:33: raise SystemExit34:35: else:36: print "Paired with %s... monitoring" % j37: connected = True38: locked = False39:40: # Send Hi every second, and detect when connection is broken41: while 1:42: try:43: client_socket.send("Hi")44:45: except IOError:46: if locked !=True:47: locked = True48: connected = False49: subprocess.call("rundll32.exe user32.dll,LockWorkStation")50:51: if connected == False:52: try:53: client_socket=BluetoothSocket( RFCOMM )54: client_socket.connect((selected_device, 3))55:56: except IOError:57: continue58:59: else:60: connected = True61:62: except KeyboardInterrupt:63: client_socket.close()64:65: else:66: locked = False67: time.sleep(1)
Cheers,
at0m q[^_^]p
i know that you said that you don't have it log back on because of precaution, but could you include the code for it to log back in when the device is near?
ReplyDelete