Sunday May 20

Archive for the ‘Python’ Category

May
17/10
Generate Random IP with Python
Last Updated on Saturday, 3 July 2010 03:43
Written by Cody Snider
Monday, May 17th, 2010

In need of an IP address on-the-fly that appears to be valid? Try this:

View Code PYTHON
from random import randrange
 
def generateIP():
    blockOne = randrange(0, 255, 1)
    blockTwo = randrange(0, 255, 1)
    blockThree = randrange(0, 255, 1)
    blockFour = randrange(0, 255, 1)
    print 'Random IP: ' + str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)
    if blockOne == 10:
        return self.__generateRandomIP__()
    elif blockOne == 172:
        return self.__generateRandomIP__()
    elif blockOne == 192:
        return self.__generateRandomIP__()
    else:
        return str(blockOne) + '.' + str(blockTwo) + '.' + str(blockThree) + '.' + str(blockFour)

We’re skipping 10.x.x.x, 172.x.x.x and 192.x.x.x due to the fact that these are reserved address. RFC 1918

Version 2:

An elegant solution to serve the purpose of generating a random IP provided by Ben (explanation of changes listed in the comments below):

View Code PYTHON
from random import randrange
if __name__=="__main__":
    not_valid = [10,127,169,172,192]
 
    first = randrange(1,256)
    while first in not_valid:
    first = randrange(1,256)
 
    ip = ".".join([str(first),str(randrange(1,256)),
    str(randrange(1,256)),str(randrange(1,256))])
    print ip
Posted under Python, SEO  |  Comments  6 Comments
Jul
18/09
WordZe API Python Script
Last Updated on Wednesday, 14 April 2010 09:56
Written by Cody Snider
Saturday, July 18th, 2009

Can be run from the command line or in another application. Built for Python 2.4 with pyCURL

View Code PYTHON
import sys, pycurl, re, time, string, urllib
from datetime import date
import xml.dom.minidom
 
class WordZe:
 
	def __init__(self, key='ADD YOUR KEY HERE'):
		self.apiKey = key
		self.caretakerObj = WordZeCaretaker()
		self.userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)'
 
	def singleKeywordLookup(self, keyList = []):
		if len(keyList) == 0:
			raise Error('You need to pass a list to the singleKeywordLookup to perform a request.')
		else:
			# TODO: DB CHECK FOR VALUE PULLED WITHIN LAST 30 DAYS
			keyString = string.join(keyList, ',')
 
			requestObj = pycurl.Curl()
 
			requestObj.setopt(pycurl.USERAGENT, self.userAgent)
			requestObj.setopt(pycurl.FOLLOWLOCATION, 1)
			requestObj.setopt(pycurl.WRITEFUNCTION, self.caretakerObj.remotecallback)
			requestObj.setopt(pycurl.HTTPGET, 1)
 
			keyString = str(keyString).replace(' ','+')
			requestObj.setopt(pycurl.URL, str('http://api.wordze.com/KeywordSingle?ApiKey=' + str(self.apiKey) + '&Query=' + str(keyString)))
			requestObj.perform()
			requestObj.close()
 
			return self.caretakerObj.getDict()
 
class WordZeCaretaker:
 
	def __init__(self):
		self.contents = ''
 
	def getDict(self):
		docMod = xml.dom.minidom.parseString(self.contents)
		return self.handleKeywords(docMod)
 
	def handleKeywords(self, docMod):
		returnDict = []
		keywords = docMod.getElementsByTagName("Keyword")
		for keyword in keywords:
			keyDict = {
			    'keyword':self.getText(keyword.childNodes),
			    'count':int(keyword.attributes['Count'].value),
			    'estimated':int(keyword.attributes['Estimated'].value)
			    }
			returnDict.append(keyDict)
		# TODO: DB INSERT
 
		return returnDict
 
	def getText(self,nodelist):
		rc = ""
		for node in nodelist:
			if node.nodeType == node.TEXT_NODE:
				rc = rc + node.data
		return rc
 
	def remotecallback(self, buf):
		self.contents = self.contents + buf
 
if __name__ == '__main__':
	lookupObj = WordZe()
	keywords = raw_input('Comma sep, no spaces list of terms: ')
	termList = str(keywords).split(',')
	cleanList = []
	for term in termList:
		cleanList.append(str(term))
	lookupObj.singleKeywordLookup(cleanList)
Posted under Python, SEO  |  Comments  2 Comments