Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Monday, June 30, 2008

Get Key Press in Python

Getting single key press without having user pres ENTER.

Windows:


import msvcrt

def getkey():
return msvcrt.getch()


Linux:

import termios, sys, os
TERMIOS = termios

def getkey():
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
new[6][TERMIOS.VMIN] = 1
new[6][TERMIOS.VTIME] = 0
termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
c = None
try:
c = os.read(fd, 1)
finally:
termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
return c



Usefull links:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197140

Friday, June 27, 2008

Simple SOCKS4 proxy in Python

If you need a SOCKS4 proxy for a custom port you can simply write it in a few python lines.


from twisted.internet import reactor
from twisted.protocols import socks

from twisted.internet.protocol import Factory

factory = Factory()
factory.protocol = socks.SOCKSv4

reactor.listenTCP(9999, factory)
reactor.run()

Wednesday, June 11, 2008

Sending Email with Python

A quick way to send email with Python.


import smtplib

msg_from = "me@email.com"
msg_to = "you@email.com"
smtp_server = "mail.mailserver.com"

msg_subject = "Hello"
msg_body = "This is message body."

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n%s" % (msg_from, msg_to, msg_subject, msg_body))

server = smtplib.SMTP(smtp_server)
server.set_debuglevel(1)
server.sendmail(msg_from, msg_to, msg)
server.quit()