Using Mechanize to Fill Out a Form
Hi, I am trying to use the Mechanize module to browse to a site, fill out a form, and login. However, it hangs on the opening of the URL. The URL is https, will this cause the open from mechanize to fail? Thanks. Kevin -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Mechanize to Fill Out a Form
On Dec 16, 3:44 pm, KDawg44 kdaw...@gmail.com wrote: Hi, I am trying to use the Mechanize module to browse to a site, fill out a form, and login. However, it hangs on the opening of the URL. The URL is https, will this cause the open from mechanize to fail? Thanks. Kevin Apparently, the HTTPS does not matter, but it is still hanging on the open(URL). I am trying to login to Yahoo!. Code below: #!/usr/bin/python import urllib import urllib2 import mechanize baseURL = 'https://login.yahoo.com/' br = mechanize.Browser() br.set_handle_robots(False) br.addheaders = [('User-Agent', 'Firefox')] #try: br.open(baseURL) It just hangs until I kill it. When I kill it, it gives me the following traceback: Traceback (most recent call last): File FantasyHockeyLoginAndGetTeam.py, line 14, in module br.open(baseURL) File build/bdist.linux-i686/egg/mechanize/_mechanize.py, line 209, in open File build/bdist.linux-i686/egg/mechanize/_mechanize.py, line 236, in _mech_open File build/bdist.linux-i686/egg/mechanize/_opener.py, line 202, in open File build/bdist.linux-i686/egg/mechanize/_http.py, line 578, in http_response KeyboardInterrupt Thanks for any advice. Kevin -- http://mail.python.org/mailman/listinfo/python-list
Sending username password to a webpage
Hi, Is there a way to essentially simulate populating a text box and calling a submit button on a webpage? I want to write an app that gets a users information from a website and then uses that to get information from another site. The first site requires a log in. Thanks for any advice that gets me in the right direction. Thanks. Kevin -- http://mail.python.org/mailman/listinfo/python-list
Distributing Python App
Hi, I am very new to Python but find it very interesting (from what I know of it) and am considering writing an application. However, I have a question regarding distribution of the app once it is done. Is there a way to package everything a user needs to run the app or will they have to install Python their machine? This is probably an obvious question but most development I do is for administrative tasks on servers and such, not usually development of an app for someone else. Thanks for your help. Kevin -- http://mail.python.org/mailman/listinfo/python-list
New to Python - Accessing Lotus Notes
Hi, We have a Lotus Notes Database that tracks time spent on projects. What I would like to do is develop a Time Tracker in Python that communicates with the server. This would pull projects in and allow a use to start a timer as he/she works on a given project. The user would then be able to commit this back to the database (thus, preventing them from having to go to the calendar view and pick the project and enter the hours). As another potential feature, I would like the user to be able to create projects in the app and track time and then when a project exists in the database, it would retroactively add those hours (users cannot create their own projects and sometimes managers take some time to actually get a project in the system). Lasty, the user could run a report by date to get their hours on each project. Anyway, long story short, how can I communicate with Lotus Notes to accomplish these tasks? Lotus Notes 7. Thank you for your help. I am very new to Python and am hoping to learn a lot from this project so I can incorporate Python into my daily tasks. Also, forgive my rant as I figured out some of my specs as I typed this. Thanks again. Kevin -- http://mail.python.org/mailman/listinfo/python-list
Script to Remove Attachments in Exchange Mailbox
Hi, I am frustrated with my users who send large files around the office instead of using the network shares. For instance, this is one of many emails I have sent around: If you take the this kit it looks like J sent it to D, D sent it to you, then you sent it to me. The problem with that is just that now there are copies of this file: On J's drive or Comm drive J's sent items D's inbox D's sent items Your inbox Your sent items My inbox The comm. Drive once I save it there to be used. Which means that there are 8 copies of the same file 4MB taking up space, or a 4MB file turned into a 32MB file. My users just aren't getting it! And then they complain when they get quota messages! (and some people, when they are Important People, you cannot argue with). So, what I would like, is to write a script that parses the exchange mailbox, and removes all attachments over a certain size (say 500K) that are attached to messages that are more than 2 weeks old, or that are in sent items. I would like to write to a log file all the changes that are made then email that log file to the mailbox that was just trimmed. I will also like to have a verbose mode that allows the user when the script is run to ok each removal. I am trying to decide if I should do this in Python or VBScript. Any suggestions? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Script to Remove Attachments in Exchange Mailbox
On Oct 11, 10:03 am, Tim Golden [EMAIL PROTECTED] wrote: KDawg44 wrote: Hi, I am frustrated with my users who send large files around the office instead of using the network shares. [...] Which means that there are 8 copies of the same file 4MB taking up space, or a 4MB file turned into a 32MB file. So, what I would like, is to write a script that parses the exchange mailbox, and removes all attachments over a certain size (say 500K) that are attached to messages that are more than 2 weeks old, or that are in sent items. I would like to write to a log file all the changes that are made then email that log file to the mailbox that was just trimmed. I have something v. similar, only it's at work and I'm not. Maybe tomorrow. What it does -- I think, it's been a while -- is to rip through any mailboxes finding attachments over a certain size, saving them to some kind of folder structure on the user's home drive and replacing the attachment by a link to the attachment. I have another script which, independently, rips through users' home shares finding duplicates and linking them to one copy. It's a while since they were last run to they're probably quite dusty but it sounds like the kind of thing you're after. TJG That sounds great! Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Learning to use wxPython
Hi, I downloaded the wxPython demo and did an emerge wxpython (gentoo) to install. When I run the demo, I am getting this error: # python demo.py Traceback (most recent call last): File /usr/lib/python2.4/site-packages/wx-2.6-gtk2-ansi/wx/ _misc.py, line 1286, in Notify self.notify() File /usr/lib/python2.4/site-packages/wx-2.6-gtk2-ansi/wx/ _core.py, line 13637, in Notify self.result = self.callable(*self.args, **self.kwargs) File /root/wxPython Demo/wxPython-2.8.3.0/demo/Main.py, line 1798, in ShowMain frame = wxPythonDemo(None, wxPython: (A Demonstration)) File /root/wxPython Demo/wxPython-2.8.3.0/demo/Main.py, line 1275, in __init__ self.filter = wx.SearchCtrl(leftPanel) AttributeError: 'module' object has no attribute 'SearchCtrl' What am I doing wrong? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning to use wxPython
On Apr 27, 11:25 am, Michele Petrazzo [EMAIL PROTECTED] wrote: KDawg44 wrote: Hi, Hi, I downloaded the wxPython demo and did an emerge wxpython (gentoo) to install. When I run the demo, I am getting this error: # python demo.py Traceback (most recent call last): File /usr/lib/python2.4/site-packages/wx-2.6-gtk2-ansi/wx/ _misc.py, line 1286, in Notify From here, I read that you are using the wx version 2.6 and the demo that you are tring are for the 2.8.3! (the last as now). So: 1) install the new 2.8 2) download the demo for the 2.6! :) Bye, Michele ahhhmy badi did not realize that. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Learning Python - First Project
Hi, I am new to Python and am trying to write a little front end to another application in Python. What I want is to have a gui pop up listing some items with several buttons. The guts of the program I am not having any trouble with but the GUI part I am (or more accurately, the transition between GUI pieces). The first GUI that pops up lists some groups in a listbox and gives the user the choice to create a new group, open a group, rename the group, or delete the group. The new group and rename group buttons pop up a dialog gui asking for the name/new name. The Open Group is to open another GUI listing projects within that group in a list with similar options (New Project, Open Project, Rename Project, Delete Project). My question is, how should I create all these GUIs? Should each GUI be its own class with its own __init__? Then is the first GUI the root (how I have it set up now) and all other GUIs using Toplevel()? I hope this makes sense (because it only sort of makes sense in my head). THanks for any suggestions. -- http://mail.python.org/mailman/listinfo/python-list
Launch another application
Hi, I would like to launch another application and pass some arguments to it from python. WHen the user clicks on the button, i want it to execute: binaryApplication fileName Is there a way to do this as if I was doing it from a shell in python? Would it be using something like popen? this app does not need to return anything to the python app i am writing and the python app does not need to wait for it (to close or do anyting else). Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Learning Python - First Project
On Apr 23, 1:44 pm, [EMAIL PROTECTED] wrote: On Apr 23, 9:52 am, KDawg44 [EMAIL PROTECTED] wrote: Hi, I am new to Python and am trying to write a little front end to another application in Python. What I want is to have a gui pop up listing some items with several buttons. The guts of the program I am not having any trouble with but the GUI part I am (or more accurately, the transition between GUI pieces). The first GUI that pops up lists some groups in a listbox and gives the user the choice to create a new group, open a group, rename the group, or delete the group. The new group and rename group buttons pop up a dialog gui asking for the name/new name. The Open Group is to open another GUI listing projects within that group in a list with similar options (New Project, Open Project, Rename Project, Delete Project). My question is, how should I create all these GUIs? Should each GUI be its own class with its own __init__? Then is the first GUI the root (how I have it set up now) and all other GUIs using Toplevel()? I hope this makes sense (because it only sort of makes sense in my head). THanks for any suggestions. I am assuming you are using Tkinter for your GUI front-end. You should be able to just use standard dialog boxes for your new group and rename group dialogs and a custom hand-coded dialog for the other one. All three can be called with ShowModal() instead of Toplevel(). And yes, the custom dialog would work best if you made it into a separate class. You could also put that information for the GUI that list projects into a tree widget of some sort, maybe with a splitter window. I haven't had much luck with Tkinter's tree widgets though. PMW and Tix both have rather poor docs unless you enjoy man pages. You might check out wxPython instead. It has an excellent demo that is very good at showing you not only what all it can do, but how it is done:www.wxpython.org. Good luck! Mike Thanks very much for your suggestions. I think i will look into wxPython, though I will finish this little app in tkinter since I am almost done with it. THanks for your help. -- http://mail.python.org/mailman/listinfo/python-list
Expanding tkinter widgets to fill the window
Hi, I am writing a GUI front end in Python using Tkinter. I have developed the GUI in a grid and specified the size of the window. The widgets are centered into the middle of the window. I would like them to fill the window. I tried using the sticky=E+W+N+S option on the widgets themselves and the window itself. How can I get this? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing log in SQL DB to change IPs to hostnames
On Apr 11, 1:15 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On 10 Apr 2007 11:54:18 -0700, KDawg44 [EMAIL PROTECTED] 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: # breakif 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 -- WulfraedDennis Lee Bieber KD6MOG [EMAIL PROTECTED] [EMAIL PROTECTED] HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: [EMAIL PROTECTED]) 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing log in SQL DB to change IPs to hostnames
On Apr 11, 11:58 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On 11 Apr 2007 05:39:21 -0700, KDawg44 [EMAIL PROTECTED] declaimed the following in comp.lang.python: 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'd be interested in the schema definition -- my experience has been that numeric fields come out of MySQLdb as numerics... import MySQLdb cn = MySQLdb.connect(host=localhost, user=BestiariaCP, db=bestiaria) cr = cn.cursor() cr.execute(select * from comics where banner is not Null) 35L cr.fetchone() (2L, 'Anxiety Cafe', 'Anxiety Cafe', 'http://rockerbot.artistrealm.org/', 'images/ronandrockerbutton.jpg', 234L, 60L, 'Platitudes from a platypus.', 'On hiatus - site rebuild', 'N', 'Y') Note the first field there -- a Python long integer (the schema defines that as an integer auto_increment field). Also the 234L, 60L, (image width/height) -- all returned as numeric. If your data is coming out as a string, I'd suspect the schema defined it as a character type. cr.description (('ID', 3, 2, 11, 11, 0, 0), ('name', 253, 33, 100, 100, 0, 0), ('sortname', 253, 33, 100, 100, 0, 0), ('URL', 253, 56, 75, 75, 0, 0), ('banner', 253, 29, 75, 75, 0, 1), ('width', 3, 3, 11, 11, 0, 1), ('height', 3, 3, 11, 11, 0, 1), ('description', 252, 170, 65535, 65535, 0, 0), ('occurs', 253, 58, 125, 125, 0, 1), ('isactive', 254, 1, 1, 1, 0, 0), ('isonline', 254, 1, 1, 1, 0, 0)) CREATE TABLE `comics` ( `ID` int(11) NOT NULL auto_increment, `name` varchar(100) NOT NULL default '', `sortname` varchar(100) NOT NULL default '', `URL` varchar(75) NOT NULL default '', `banner` varchar(75) default NULL, `width` int(11) default NULL, `height` int(11) default NULL, `description` text NOT NULL, `occurs` varchar(125) default NULL, `isactive` enum('N','Y') NOT NULL default 'Y', `isonline` enum('N','Y') NOT NULL default 'Y', PRIMARY KEY (`ID`), KEY `namesort` (`sortname`) ) ENGINE=MyISAM AUTO_INCREMENT=92 DEFAULT CHARSET=latin1; -- WulfraedDennis Lee Bieber KD6MOG [EMAIL PROTECTED] [EMAIL PROTECTED] HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: [EMAIL PROTECTED]) HTTP://www.bestiaria.com/ I see your point. Somewhere in my head I must have mixed them up. It goes in as a string but comes out as whatever data type. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Parsing log in SQL DB to change IPs to hostnames
Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to parse the syslog messages and try to change any ips to resolved hostnames. Unfortunately, I am not getting any matches on my regular expression. A message will look something like this: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: 1.1.1.1 Accessed URL 10.10.10.10:/folder/folder/page.html I would like to change the message to have the hostnames, or even better actually, have it appear as hostname-ip address. So a changed message would look like: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: pcname-1.1.1.1 Accessed URL www.asite.com-10.10.10.10:/folder/folder/page.html or some equivalent. Here is what i have so far. Please be kind as it is my first python program :) #! /usr/bin/python import socket import re import string import MySQLdb 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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM `logs` WHERE seq = 507702) # one specific message so that it doesn't parse the whole DB during testing... while(1): row = cursor.fetchone() if row == None: break if ipRegEx.match(row[0]): print regex match! + ipRegEx.match(row[0]) # does not make it here. newMsg = ipRegEx.sub(query(ipRegEx.match(row[0])), row[0]) if newMsg != 0: cursor.execute( UPDATE logs SET msg = %s WHERE seq = d, (newMsg,row[1])) def query(ipAddress): try: return socket.gethostbyaddr(ipAddress)[0] except: return 0 This is built to sub the name... I would like to change it to the hsotname-ipaddress instead since ip's can resolve to many hostnames it runs but does not pass the if statements so it quietly does absolutely nothing :) Thanks so much for any advice -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing log in SQL DB to change IPs to hostnames
On Apr 10, 11:11 am, Kushal Kumaran [EMAIL PROTECTED] wrote: On Apr 10, 8:37 pm, KDawg44 [EMAIL PROTECTED] wrote: Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to parse the syslog messages and try to change any ips to resolved hostnames. Unfortunately, I am not getting any matches on my regular expression. A message will look something like this: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: 1.1.1.1 Accessed URL 10.10.10.10:/folder/folder/page.html I would like to change the message to have the hostnames, or even better actually, have it appear as hostname-ip address. So a changed message would look like: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: pcname-1.1.1.1 Accessed URLwww.asite.com-10.10.10.10:/folder/folder/page.html or some equivalent. Here is what i have so far. Please be kind as it is my first python program :) #! /usr/bin/python import socket import re import string import MySQLdb 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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM REMOVED WHERE seq = 507702) # one specific message so that it doesn't parse the whole DB during testing... while(1): row = cursor.fetchone() if row == None: break if ipRegEx.match(row[0]): snipped rest of the code See the documentation of the re module for the difference between matching and searching. -- Kushal Thank you very much. I think I have it figured out, except for an error on the SQL statement: [- 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 ---] #! /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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM `logs` WHERE seq = 507702) while(1): row = cursor.fetchone() 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 = %i, (newMsg,seqNum)) [- END CODE ---] Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing log in SQL DB to change IPs to hostnames
On Apr 10, 1:54 pm, KDawg44 [EMAIL PROTECTED] wrote: On Apr 10, 11:11 am, Kushal Kumaran [EMAIL PROTECTED] wrote: On Apr 10, 8:37 pm, KDawg44 [EMAIL PROTECTED] wrote: Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to parse the syslog messages and try to change any ips to resolved hostnames. Unfortunately, I am not getting any matches on my regular expression. A message will look something like this: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: 1.1.1.1 Accessed URL 10.10.10.10:/folder/folder/page.html I would like to change the message to have the hostnames, or even better actually, have it appear as hostname-ip address. So a changed message would look like: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: pcname-1.1.1.1 Accessed URLwww.asite.com-10.10.10.10:/folder/folder/page.html or some equivalent. Here is what i have so far. Please be kind as it is my first python program :) #! /usr/bin/python import socket import re import string import MySQLdb 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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM REMOVED WHERE seq = 507702) # one specific message so that it doesn't parse the whole DB during testing... while(1): row = cursor.fetchone() if row == None: break if ipRegEx.match(row[0]): snipped rest of the code See the documentation of the re module for the difference between matching and searching. -- Kushal Thank you very much. I think I have it figured out, except for an error on the SQL statement: [- 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 ---] #! /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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM `logs` WHERE seq = 507702) while(1): row = cursor.fetchone() 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 = %i, (newMsg,seqNum)) [- END CODE ---] Thanks again! Also, i tried changing seqNum = row[1] to seqNum = int(row[1]) to cast it as an integer and I get the same error (because I think that pulling from a DB makes everything a string by default?) Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing log in SQL DB to change IPs to hostnames
On Apr 10, 2:47 pm, Steve Holden [EMAIL PROTECTED] wrote: KDawg44 wrote: On Apr 10, 11:11 am, Kushal Kumaran [EMAIL PROTECTED] wrote: On Apr 10, 8:37 pm, KDawg44 [EMAIL PROTECTED] wrote: Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to parse the syslog messages and try to change any ips to resolved hostnames. Unfortunately, I am not getting any matches on my regular expression. A message will look something like this: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: 1.1.1.1 Accessed URL 10.10.10.10:/folder/folder/page.html I would like to change the message to have the hostnames, or even better actually, have it appear as hostname-ip address. So a changed message would look like: Apr 10 2007 00:30:58 DEVICE : %DEVICEINFO: pcname-1.1.1.1 Accessed URLwww.asite.com-10.10.10.10:/folder/folder/page.html or some equivalent. Here is what i have so far. Please be kind as it is my first python program :) #! /usr/bin/python import socket import re import string import MySQLdb 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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM REMOVED WHERE seq = 507702) # one specific message so that it doesn't parse the whole DB during testing... while(1): row = cursor.fetchone() if row == None: break if ipRegEx.match(row[0]): snipped rest of the code See the documentation of the re module for the difference between matching and searching. -- Kushal Thank you very much. I think I have it figured out, except for an error on the SQL statement: [- 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 ---] #! /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: %d - %s % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() cursor.execute(SELECT msg, seq FROM `logs` WHERE seq = 507702) while(1): row = cursor.fetchone() 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 = %i, (newMsg,seqNum)) [- END CODE ---] Thanks again! Since the source line that the traceback complains about doesn't appear in the quoted code it's difficult to know what's going wrong. I'd hazard a guess that you have a string in seqNum instead of an integer message number (in which case try using int(seqNum) instead). Otherwise show us the real code, not the one after you modified it to try and make it work, amd we might be able to help more ;-) regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenwebhttp://del.icio.us/steve.holden Recent Ramblings http://holdenweb.blogspot.com hmmm... I tried seqNum = int(seqNum[i]) to make seqNum an integer. That is the real code with the connection to the DB hidden and the name of the table which I didnt remove on the first query anyway so it was pointless. This is the whole code. THanks. -- http://mail.python.org/mailman/listinfo/python-list
Using Python to start SNMP service on remote XP hosts...
Hi, I am very new to Python and really just began studying and using it. I read that it was relatively easy to interact with Windows machines with Python and I am desperately looking for something to replace VBScript (not a big fan). I am currently deploying a Gentoo Linux SNMP server running Cacti in my network and would like my XP machines to report traps to this box. The SNMP service is not running by default on the machines in the network and instead of going to each one at a time, I thought I could write a script. Does anyone have any suggestions about how I go about accomplishing this? Thanks so much for any suggestions. -- http://mail.python.org/mailman/listinfo/python-list
Beginner question: difference between lists and tuples
Hi, I am trying to learn python. I am working through a tutorial on python.org. I am trying to figure out how lists are different than tuples other than changing values at specific indices. How are these different from arrays in other languages such as php? THanks. -- http://mail.python.org/mailman/listinfo/python-list