Parsing log in SQL DB to change IPs to hostnames
KDawg44
KDawg44 at gmail.com
Wed Apr 11 08:39:21 EDT 2007
On Apr 11, 1:15 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On 10 Apr 2007 11:54:18 -0700, "KDawg44" <KDaw... at gmail.com> declaimed
> the following in comp.lang.python:
>
>
>
> > [----- BEGIN ERROR ---]
> > Traceback (most recent call last):
> > File "changeLogs.py", line 47, in ?
> > cursor.execute("""UPDATE logs SET msg = %s WHERE seq = %i""",
> > (newMsg,seqNum))
> > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
> > 148, in execute
> > query = query % db.literal(args)
> > TypeError: int argument required
> > [----- END ERROR ---]
>
> > Here is my code
>
> > [----- BEGIN CODE ---]
>
> Comments interspersed
>
>
>
> > #! /usr/bin/python
>
> > import socket
> > import sys
> > import re
> > import string
> > import MySQLdb
>
> > def resolveHost(ipAdds):
> > ipDict = {}
> > for ips in ipAdds:
> > try:
> > ipDict[ips] = socket.gethostbyaddr(ips)[0]
> > except:
> > ipDict[ips] = "Cannot resolve"
> > return ipDict
>
> > ipRegExC = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
> > ipRegEx = re.compile(ipRegExC)
>
> > try:
> > conn = MySQLdb.connect(host="REMOVED",
> > user="REMOVED",
> > passwd="REMOVED",
> > db="REMOVED")
>
> > except MySQLdb.Error, e:
> > print "Error connecting to the database: %s - %s " % (e.args[0], e.args[1])
>
> #just use %s unless you need particular numeric formatting
> #(field width and decimal places, as in %8.4f)> sys.exit(1)
>
> > cursor = conn.cursor()
>
> cursor.execute("SELECT msg, seq FROM logs WHERE seq = 507702")
> #why the ` around the table name?
> #I also presume at some point that 507702 becomes dynamic -- otherwise
> the returned
> #seq field will always be 507702,-- and if there are multiple instances,
> the update
> #statement below will change all of them each time
>
> #while(1):
> # row = cursor.fetchone()
> for row in cursor:> ipAddresses = []
> > resolvedDict = {}
>
> # if row == None:
> # break> if ipRegEx.search(row[0]):
> > seqNum = row[1]
> > ipAddresses = ipRegEx.findall(row[0])
> > resolvedDict = resolveHost(ipAddresses)
> > newMsg = row[0]
> > for ip in resolvedDict.keys():
> > newMsg = newMsg.replace(ip,
> > ip + "-" +resolvedDict[ip])
>
> cursor.execute(
> """UPDATE REMOVED SET msg = %s WHERE seq = %s""",
> (newMsg, seqNum))
> #the documented parameter code for MySQLdb is %s; use of
> anything
> #else relies upon luck -- luck that the safety logic inside
> the module
> #leaves the parameter in a format compatible with the format
> code!
> #%s is the only code you should use with MySQLd --
> regardless of
> #the type of the actual data
>
> --
> Wulfraed Dennis Lee Bieber KD6MOG
> wlfr... at ix.netcom.com wulfr... at bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-a... at bestiaria.com)
> HTTP://www.bestiaria.com/
ohhh okay. thanks so much. I knew that it came out as strings, i
guess it makes sense that I would have to send it back IN as a
string. Changed that and now it works! THanks so much.
I just specified once specific field so that it would not change the
whole db on a test run.
Thanks so much.
More information about the Python-list
mailing list