import struct
import socket
class IPIterator:
def __init__(self, ip, bitmask):
self.ip = ip
self.bitmask = bitmask
def __iter__(self):
ipnum = struct.unpack(">L", socket.inet_aton(self.ip))[0]
mask = 2 ** self.bitmask - 1
for x in xrange(ipnum & ~mask, ipnum | mask):
yield socket.inet_ntoa(struct.pack(">L", x))
if __name__ == "__main__":
for x in IPIterator("1.2.3.4", 8):
print x
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Wednesday, July 29, 2009
Iterating over a sequence of IPs in Python
I was bit bored, so I wrote a simple iterator that iterates over a sequence of IPs using a subnet bitmask.
Friday, February 6, 2009
Playing with signals in Python
A Python program is able to catch signals and handle them with the signal module. This is especially useful when you want to clean up after your program exits for any reason. This can be done with signal.signal, which sets a callback function for signal handlers.
This code will set an "alarm" that must be set every 30 seconds, or else a signal will be called to shut down the program (sort of a "watchdog" program).
Another use of signals would be to brutally terminate your program, since sometimes Python can hang on I/O, or a Thread object that is running (for some reason a Ctrl+C doesn't kill a program running a python thread):
This code will set an "alarm" that must be set every 30 seconds, or else a signal will be called to shut down the program (sort of a "watchdog" program).
def alarm_handler(signum, frame):
print "30 seconds have passed!"
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(30)
while True:
data = raw_input("Write something in the next 30 seconds please: ")
signal.alarm(30)
Another use of signals would be to brutally terminate your program, since sometimes Python can hang on I/O, or a Thread object that is running (for some reason a Ctrl+C doesn't kill a program running a python thread):
import os, signal
# Brutally terminate this process
os.kill(0, signal.SIGTERM)
Sending E-Mail using Python
Well, it's really easy, all you need to do is build up a MIME message using the Python "email" library.
I am using the "alternative" multipart content type, which allows be to enter both text content and HTML content, and display the supported content type (which will of course almost always be HTML, but there are a lot of guys who still use text clients to read emails apparently).
I also used a small line in the example that convert from simple text to HTML.
Here is a sample code:
I am using the "alternative" multipart content type, which allows be to enter both text content and HTML content, and display the supported content type (which will of course almost always be HTML, but there are a lot of guys who still use text clients to read emails apparently).
I also used a small line in the example that convert from simple text to HTML.
Here is a sample code:
#!/usr/bin/python
import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# We are using the local SMTP server
MAIL_SERVER = "localhost"
class Mailer:
def __init__(self):
self.mail_server = MAIL_SERVER
def send_raw_email(self, mail_from, mail_to, data):
smtp = smtplib.SMTP(self.mail_server)
smtp.sendmail(mail_from, mail_to, data)
smtp.quit()
def send_email(self, mail_from, mail_to, subject, text_message, html_message='', images=[]):
# Initialize MIME message
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = mail_from
if type(mail_to) is str:
msg["To"] = mail_to
else:
msg["To"] = ", ".join(mail_to)
text = MIMEText(text_message)
msg.attach(text)
if html_message:
html = MIMEText(html_message, "html")
msg.attach(html)
for image in images:
image = MIMEImage(open(image, "rb"))
msg.attach(image)
self.send_raw_email(mail_from, mail_to, msg.as_string())
if __name__ == '__main__':
mailer = Mailer()
subject = "This is the subject"
message = "This is the message"
html = "<html dir="'ltr'"><body>%s</body></html>" % message.replace("\n", "<br/>")
mailer.send_email("admin@myserver.com", "recipient@example.com", subject, message, html)
Tuesday, January 6, 2009
Writing webbots using Python
If you ever wanted to write a webbot and didn't know how, it could be easily achieved using the mechanize module.
There is one thing that mechanize does which doesn't always suit my needs, which is to pay attention to the robots.txt file, so I just disable it.
Mechanize allows you to easily browse, extract data and submit forms.
There is one thing that mechanize does which doesn't always suit my needs, which is to pay attention to the robots.txt file, so I just disable it.
Mechanize allows you to easily browse, extract data and submit forms.
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("http://www.google.com")
for link in br.links():
print link
Subscribe to:
Posts (Atom)