[Tutor] IP-range
Hello, I came across your answer / assistance on the IP range. I am fairly new to the python world of programming. However, up to this point I have always been able to get my programs to work by reading the books or following the guides I find through google.com Here is what I have to do: I have to read a number of cvs files into 1 large file. (I have been able to get that done through a perl script). But after I get it into a large cvs file, I need to be able to look at each row and see if it falls within a specific IP ranges. IP Ranges: 162.x.x.x 151.130.x.x 145.x.x.x These are just some examples of the IP ranges. The csv file has data like below: 63.145.40.32 Gnutella File Search 14 5/15/2009 0:48 151.40.133.25 Gnutella File Search 14 5/14/2009 16:21 145.133.19.147 BitTorrent Client Activity 13 5/14/2009 19:20 Here is my source code so far: import sys import win32api import win32ui import shutil import string import os import os.path import csv #Global Variables P2Pfiles = [] #still in the development process -- where to get the files from #right now the location is C:\P2P def getp2preportdestion(): win32ui.MessageBox('Welcome to P2P Reporting.\nThis script is designed to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in C:\P2P \n\nWith no subdirectories.\n\n\n\n\nVersion 1.0 - \n\nPress "OK" to continue with this script.') #p2pdrive=raw_input("Enter the drive letter: ") #p2pdir=raw_input("Enter the directory name: ") #p2preport = p2pdrive +':\\'+p2pdir+'/' p2preport = 'C://P2P\\' return p2preport #Main Program #Get location of directories p2ploc = getp2preportdestion() #Checking to make sure directory is there. if os.path.exists(p2ploc): if os.path.isfile(p2ploc +'/p2pmerge.csv'): win32ui.MessageBox('Merge File exists..will continue with program') else: win32ui.MessageBox('Merge File does not exists, script will exit. Please run the perl script first.') exit() else: win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease create and copy all the files there.\nThen re-run this script') exit() reader = csv.reader(open('C://P2P/p2pmerge.csv', "rb")) p2pwriter = csv.writer(open('C://P2P\P2PMerge4.csv', "wb")) for row in reader: p2pwriter.writerow([row]) win32ui.MessageBox('I am here') Any assistance will be greatly appricated!!! Thanx, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] CVS File Opening
Hello, I have been working on this script / program all weekend. I emailed this address before and got some great help. I hope that I can get that again! First to explain what I need to do: Have about 6 CSV files that I need to read. Then I need to split based on a range of IP address and if the count number is larger than 75. I currently merge all the CSV files by using the command line: C:Reports> copy *.csv merge.csv Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "." merge.csv ^| find /v ""') do echo %i>> P2PMerge.csv >From some of my friends they tell me that should remove that last carriage return, which it does, however when it goes through the python script it returns no values. Now if I open the merge.csv and remove that carriage return manually and save it as P2PMerge.csv the script runs just fine. Here is my source code: # P2P Report / Bitorrent Report # Version 1.0 # Last Updated: May 26, 2009 # This script is designed to go through the cvs files and find the valid IP Address # Then copys them all to a new file import sys import win32api import win32ui import shutil import string import os import os.path import csv #Global Variables P2Pfiles = [] totalcount = 0 t = 0 #still in the development process -- where to get the files from #right now the location is C:\P2P def getp2preportdestion(): win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is designed to aid in the P2P reporting. \n\nThe locations of P2P Reports should be in C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to continue with this program.') p2preport = 'C://P2P\\' return p2preport #Main Program #Get location of directories p2ploc = getp2preportdestion() #Checking to make sure directory is there. if os.path.exists(p2ploc): if os.path.isfile(p2ploc +'/p2pmerge.csv'): win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill continue with P2P Reporting.') else: win32ui.MessageBox('P2PMerge.csv files does not exists. \n\nPlease run XXX.bat files first.') sys.exit() else: win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease create and copy all the files there.\nThen re-run this script') sys.exit() fh = open('C://P2P/P2PMerge.csv', "rb") ff = open('C://P2P/P2PComplete.csv', "wb") igot1 = fh.readlines() for line in igot1: readline = line ipline = readline ctline = readline count = ctline.split(',')[2] count2 = int(count) print count2 t = count2 ip = ipline.split(' ')[0] split_ip = ip.split('.') if ((split_ip[0] == '192') and (t >=75)): ff.write(readline) totalcount +=1 elif ((split_ip[0] == '151') and (t >=75)): ff.write(readline) totalcount +=1 elif (((split_ip[0] == '142') and (split_ip[1]) == '152') and (t >=75)): ff.write(readline) totalcount +=1 tc = str(totalcount) win32ui.MessageBox('Total Number of IPs in P2P Reporting: '+ tc) fh.close() ff.close() What I am looking for is an working example of how to go through the directory and read each csv file within that directory or how to remove the carriage return at the end of the csv file. NOTE: This is not for a class - it is for work to assist me in reading multiple csv files within a couple days. Any assistance is greatly appreciated. -- Paras Kinariwala ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CVS File Opening
As requested - here is some example rows from the csv files: 117.86.68.157 BitTorrent Client Activity 1 5/21/2009 6:56 82.210.106.99 BitTorrent Client Activity 1 5/20/2009 12:39 81.132.134.83 BitTorrent Client Activity 1 5/21/2009 3:14 The rows are: IP, Activity, Count, Date / Time these are typical log files. On Tue, May 26, 2009 at 6:51 PM, Sander Sweers wrote: > 2009/5/26 Paras K. : > > Hello, > > > > I have been working on this script / program all weekend. I emailed this > > address before and got some great help. I hope that I can get that again! > > > > > > First to explain what I need to do: > > > > Have about 6 CSV files that I need to read. Then I need to split based on > a > > range of IP address and if the count number is larger than 75. > > > > I currently merge all the CSV files by using the command line: > > > > C:Reports> copy *.csv merge.csv > > > > Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in ('find "." > > merge.csv ^| find /v ""') do echo %i>> P2PMerge.csv > > > > From some of my friends they tell me that should remove that last > carriage > > return, which it does, however when it goes through the python script it > > returns no values. > > Why would you need to strip off a carriage return? And why would you > not process the csv files one after another? It would be easier to > have some example data. > > > Now if I open the merge.csv and remove that carriage return manually and > > save it as P2PMerge.csv the script runs just fine. > > > > Here is my source code: > > > > # P2P Report / Bitorrent Report > > # Version 1.0 > > # Last Updated: May 26, 2009 > > # This script is designed to go through the cvs files and find the valid > IP > > Address > > # Then copys them all to a new file > > import sys > > import win32api > > import win32ui > > import shutil > > import string > > import os > > import os.path > > import csv > > You import csv but do not use it below? > > > #Global Variables > > P2Pfiles = [] > > totalcount = 0 > > t = 0 > > #still in the development process -- where to get the files from > > #right now the location is C:\P2P > > def getp2preportdestion(): > > win32ui.MessageBox('Welcome to P2P Reporting.\nThis program is > designed > > to aid in the P2P reporting. \n\nThe locations of P2P Reports should be > in > > C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" to > continue > > with this program.') > > p2preport = 'C://P2P\\' > > return p2preport > > > > > > #Main Program > > #Get location of directories > > p2ploc = getp2preportdestion() > > #Checking to make sure directory is there. > > if os.path.exists(p2ploc): > > if os.path.isfile(p2ploc +'/p2pmerge.csv'): > > win32ui.MessageBox('P2PMerge.csv file does exists.\n\nWill > continue > > with P2P Reporting.') > > else: > > win32ui.MessageBox('P2PMerge.csv files does not exists. > \n\nPlease > > run XXX.bat files first.') > > sys.exit() > > else: > > win32ui.MessageBox('The C:\P2P directory does not exists.\n\nPlease > > create and copy all the files there.\nThen re-run this script') > > sys.exit() > > fh = open('C://P2P/P2PMerge.csv', "rb") > > ff = open('C://P2P/P2PComplete.csv', "wb") > > igot1 = fh.readlines() > > > > for line in igot1: > > You can also write the below and get rid of igot1. > for line in fh.readlines(): > > > readline = line > > ipline = readline > > ctline = readline > > You are making variables to the same object and all are not necessary. > See below idle session which should show what I mean. > > >>> line = [1,2,3,4] > >>> readline = line > >>> ipline = readline > >>> ctline = readline > >>> line > [1, 2, 3, 4] > >>> line.append('This will be copied to readline, iplin and ctline') > >>> readline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ipline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > >>> ctline > [1, 2, 3, 4, 'This will be copied to readline, iplin and ctline'] > > > count = ctline.split(',')[2] > > count2 = int(count) > >
Re: [Tutor] CVS File Opening
There are no headers for the log files, and there are mulitple log files so what that walk through the directory for all csv files? THANK IN ADVANCE!!! On Wed, May 27, 2009 at 10:18 AM, Christian Witts wrote: > Paras K. wrote: > >> As requested - here is some example rows from the csv files: >> >> 117.86.68.157 BitTorrent Client Activity 1 5/21/2009 6:56 >> 82.210.106.99 BitTorrent Client Activity 1 5/20/2009 12:39 >> 81.132.134.83 BitTorrent Client Activity 1 5/21/2009 3:14 >> >> The rows are: IP, Activity, Count, Date / Time these are typical log >> files. >> >> >> On Tue, May 26, 2009 at 6:51 PM, Sander Sweers >> > sander.swe...@gmail.com>> wrote: >> >>2009/5/26 Paras K. mailto:para...@gmail.com>>: >> >>> Hello, >>> >>> I have been working on this script / program all weekend. I >>emailed this >>> address before and got some great help. I hope that I can get >>that again! >>> >>> >>> First to explain what I need to do: >>> >>> Have about 6 CSV files that I need to read. Then I need to split >>based on a >>> range of IP address and if the count number is larger than 75. >>> >>> I currently merge all the CSV files by using the command line: >>> >>> C:Reports> copy *.csv merge.csv >>> >>> Then I run the dos command: for /F "TOKENS=* SKIP=1" %i in >>('find "." >>> merge.csv ^| find /v ""') do echo %i>> P2PMerge.csv >>> >>> From some of my friends they tell me that should remove that >>last carriage >>> return, which it does, however when it goes through the python >>script it >>> returns no values. >> >>Why would you need to strip off a carriage return? And why would you >>not process the csv files one after another? It would be easier to >>have some example data. >> >>> Now if I open the merge.csv and remove that carriage return >>manually and >>> save it as P2PMerge.csv the script runs just fine. >>> >>> Here is my source code: >>> >>> # P2P Report / Bitorrent Report >>> # Version 1.0 >>> # Last Updated: May 26, 2009 >>> # This script is designed to go through the cvs files and find >>the valid IP >>> Address >>> # Then copys them all to a new file >>> import sys >>> import win32api >>> import win32ui >>> import shutil >>> import string >>> import os >>> import os.path >>> import csv >> >>You import csv but do not use it below? >> >>> #Global Variables >>> P2Pfiles = [] >>> totalcount = 0 >>> t = 0 >>> #still in the development process -- where to get the files from >>> #right now the location is C:\P2P >>> def getp2preportdestion(): >>> win32ui.MessageBox('Welcome to P2P Reporting.\nThis program >>is designed >>> to aid in the P2P reporting. \n\nThe locations of P2P Reports >>should be in >>> C:\P2P \nWith no subdirectories.\n\nVersion 1.0 - \n\nPress "OK" >>to continue >>> with this program.') >>> p2preport = 'C://P2P\\' >>> return p2preport >>> >>> >>> #Main Program >>> #Get location of directories >>> p2ploc = getp2preportdestion() >>> #Checking to make sure directory is there. >>> if os.path.exists(p2ploc): >>> if os.path.isfile(p2ploc +'/p2pmerge.csv'): >>> win32ui.MessageBox('P2PMerge.csv file does >>exists.\n\nWill continue >>> with P2P Reporting.') >>> else: >>> win32ui.MessageBox('P2PMerge.csv files does not exists. >>\n\nPlease >>> run XXX.bat files first.') >>> sys.exit() >>> else: >>> win32ui.MessageBox('The C:\P2P directory does not >>exists.\n\nPlease >>> create and copy all the files there.\nThen re-run this script') >>> sys.exit() >>> fh = open('C://P2P/P2PMerge.csv', "rb") >>> ff = open('C://P2P
[Tutor] Converting a Py2exe Script to a Windows Installer
I have been writing many scripts using python and then making them a standalone script using Py2exe. I have the source code for the script. What I am trying to do is, take this script and make it into a windows installer and have a shortcut within the All Programs or on the desktop. Any tutorials or sites out there that will show me how to do this? Let me know. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting Data from a Web Page
First let me say this mailing list is GREAT!!! Has helped me out through many things. I have written some code with Python that currently goes through a directory of all csv files and pulls out the lines that are needed by this program. These csv files have the following information: IP Address, Activity, Count, Date Currently the code looks at the IP address and if it is part of the range overall range then it writes it to a new csv file. I have two things that I would like to improve on: 1) If the IP address falls within a specific range I want it to add an indicator at the end of it like -- NONDHCP IP (I believe I know how to do this, but any suggestion would be great) 2) If the IP is a DHCP IP -- I want it to be able to get the data from a webpage -- this webpage has like user information and etc. Is there any way to do that? Thank You for all your help in advance!!! -- Paras ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to Split a String
What I am trying to do is find the mail folder for our lotus notes files. I get it by doing the following: lotusnotesmaildir = glob.glob('C:\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data/'+'*mail*/') That returns: ['C:\\Documents and Settings/pkinariwala/Local Settings/Application Data/lotus/notes/data\\mail02\\'] What I am trying to do is split the mail folder, in this case it would be mail02, but not all users would have that same mail folder. How can I split it so I get mail02. Any help in greatly appreciated. Thanx, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding Value to CSV
I have some code that is going through a number of test. When I have the line that has been read I need to add another value at the end of it, or write the information into another csv file Example of my code: for line in fh.readlines(): readline = line ipline = readline ip = ipline.split(' ')[0] split_ip = ip.split('.') if ((split_ip[0] == '"152')): ff.write(readline) totalcount +=1 I need to add another piece, how can I add another field at the end of ff.write(readline) Any assistance would be greatly appreciated. Thank You. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding Value to CSV
What I am trying to do is as I am writing the row to the CSV file, I want to add the string base on a few other checks that I still need to write. Ex. readline = '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 15:40:33"\r\n' At the end of this based on my checks I want to be able to write a string like Part of DHCP Pool or Part of VPN Pool So the final line should be something like this written to the CSV file: '"152.88.91.98","BitTorrent Client Activity","1","2009-09-23 15:40:33", "Part of DHCP Pool" Thanx in advance for the help. On Sun, Nov 1, 2009 at 7:16 AM, Dave Angel wrote: > Paras K. wrote: > >> I have some code that is going through a number of test. >> >> When I have the line that has been read I need to add another value at the >> end of it, or write the information into another csv file >> >> Example of my code: >> >> for line in fh.readlines(): >>readline = line >>ipline = readline >>ip = ipline.split(' ')[0] >>split_ip = ip.split('.') >>if ((split_ip[0] == '"152')): >>ff.write(readline) >>totalcount +=1 >> >> >> I need to add another piece, how can I add another field at the end of >> ff.write(readline) >> >> >> Any assistance would be greatly appreciated. Thank You. >> >> >> > If your program is correct so far, then you could add it simply with: >ff.write(readline + " " + newdata) > > Although your message subject says CSV, it looks like you're breaking the > line up by spaces. So if in fact each field is constrained not to have a > space within, and there is a single space between fields, then the above > will work. > > If, on the other hand, you have to deal with fields that can contain the > delimiter, perhaps escaped or quoted, then things get much more complicated. > > DaveA > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor