screen_time_out = 30 # give here the minutes for the screen time out. the datatype should be integer.

time_out = screen_time_out*60

import subprocess
import os

bashScript = """#!/bin/bash
currentuser=`ls -l /dev/console | cut -d " " -f 4`
sudo -u $currentuser defaults -currentHost write com.apple.screensaver idleTime -int "%s"

sudo -u $currentuser defaults -currentHost read com.apple.screensaver idleTime  """%(time_out)

def execute(arguments):
    p = subprocess.Popen(arguments, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
    output, error = p.communicate()
    if output is not None and output != '':
        print "ScreenSaver setting has been set successfully"

path = "/Library/Caches/screensaver.sh"


with open(path,'wb') as f:
    f.write(bashScript)

execute("sh %s"%(path))
os.remove(path)