Arquivo para script

Python: parando script com CTRL-C no Windows

Posted in python with tags , , , , , , , on agosto 25, 2009 by Jack

Ao rodar um script python no Windows é preciso os recursos do módulo win32api para conseguir parar a execução digitando CTRL-C.  O pywin32, que provê o módulo win32api para o Python, pode ser obtido no sourceforge.net pelo link: http://sourceforge.net/projects/pywin32/files/

No Linux, quem faz isso é o os.kill(pid) e funciona de maneira nativa.

Segue um exemplo de código:

Código Python:

import win32api
import os
import time

# Obtem o numero do processo PID
process = os.getpid()
# mostra ele na tela (so pra saber)
print "Processo rodando sob PID %d" % process

# Chama um handle para o script
handle = win32api.OpenProcess(1, False, process)

# Aqui vai o resto das funcoes do script
# como exemplo um loop infinito que fica
# imprimindo Linux RLZ na tela

def LoopPrint():
  while 1:
    print “Linux RLZ – CTRL-C para sair.”
    time.sleep(.5)

if __name__==’__main__’:
  try:
    LoopPrint()
  except KeyboardInterrupt:
    win32api.TerminateProcess(handle, -1)
    win32api.CloseHandle(handle)

Referência:
http://www.python.org/doc/faq/windows/#id10

Anúncios

Xchat com python script GeoIP

Posted in Linux, Planeta with tags , , , on janeiro 20, 2009 by Jack
Xchat com python script GeoIP

# cat ~/.xchat2/geoiplookup.py
try:
  import xchat
except:
  print "O modulo xchat soh roda com o xchat"

try:
  import GeoIP
except:
  print "GeoIP module not installed."

import os
import re
import time
import string
# Identificacao
__module_name__ = "xSalada - seu xchat automatico"
__module_version__ = "0.1"
__module_description__ = "Script que retorna a origem do host"
__module_author__ = "jack at icatorze dot com dot br"

###############################
# Get nick country, if possible
###############################
def ipFormatChk(ip_str):
    try:
        num = string.split(ip_str,'.')
	if not len(num) == 4:
	    return False
	else:
	    for n in num:
	    	try:
		    if int(n)  255:
		        return False
		except:
		    return False
            return True
    except:
        return False

def getCountry(word, word_eol, userdata):
    print word
    users = xchat.get_list("users")
    try:
        gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
        try:
            nicktoquery = word[1]
        except:
            print "Choose a nick"
	    nicktoquery = ""
        for user in users:
            if user.nick == nicktoquery:
	       partes_host = string.split(user.host,'@',1)
	       hostid = partes_host[1]
	       if ipFormatChk(hostid):
	           print "%s from %s" % (nicktoquery,gi.country_name_by_addr(hostid))
               elif re.match(r'.*/.*', hostid):
		   print "%s cloaked as %s" % (nicktoquery,hostid)
               else:
                   print "%s from %s" % (nicktoquery,gi.country_name_by_name(hostid))
    except:
    	print "GeoIP nao instalado"
    return xchat.EAT_ALL

def getCountryJoin(word, word_eol, userdata):
    #print word[2]
    try:
        gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
        partes_host = string.split(word[2],'@',1)
        hostid = partes_host[1]
        if ipFormatChk(hostid):
            print "%s from %s" % (word[0],gi.country_name_by_addr(hostid))
        elif re.match(r'.*/.*', hostid):
	    print "%s cloaked as %s" % (word[0],hostid)
        else:
            print "%s from %s" % (word[0],gi.country_name_by_name(hostid))
    except:
        print "GeoIP nao instalado"
    return xchat.EAT_NONE

try:
    xchat.hook_print("Join", getCountryJoin)
    xchat.hook_command("geoip",getCountry)

except:
    print "modo ajuda ou deu merda!!!"

if __name__ == '__main__':
    pass
%d blogueiros gostam disto: