Page 1 of 3

Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 6:23 pm
by centip3de
Alright, so this is pretty noobish, and somewhat embarrassing, but I've been racking my brains, books, and Google trying to find an answer. So basically, I devised a way to control my computer via text messages. For instance, if I send a text that says start "hackthissite.py" it would open a file called "hackthissite.py" which would open a browser directed to hackthissite.org. Not too shabby eh? ;) It uses mozilla thunderbird and an add-on for it. (If anyones interested I'll go into greater detail)


Anyway! Back to the crux of the matter. I don't want to use the other programs, because really their just a crutch :/ . So far I've gotten it to retrieve my mailbox, get the emails, place them in a text file and read them. Here's what I want to happen (Mixed with my current code):

Code: Select all
import getpass, poplib, sys, os, time

Mailbox = poplib.POP3_SSL("pop.email_provider.com", 995)
Mailbox.user("MY_EMAIL")
Mailbox.pass_("MY_PASS")
messageCount = len(Mailbox.list() [1])
for i in range(messageCount):
      for j in Mailbox.retr(i+1) [1]:
            ff = open('Mail.txt', 'a')
            ff.write(j)
            ff.close()
                     
infile = open('Mail.txt', "r")
text = infile.read()
infile.close
search = "test"
index = text.find(search)
if text.content(search):
Do cool stuffz here


The "if text.content(search):" Part is pseudo code obv. Anywho, thats the wishful thinking part, the ability to say if "Mail.txt" has "search" in it, then do cool stuffz. But I can't code the if statment -.-

Help a noob pl0x :D

Thanks.

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 6:50 pm
by fashizzlepop
Just a guess but I'm thinking you need to use a regular expression. Try googleing for Python and reg ex.

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 7:02 pm
by insomaniacal
Theres probably an easier way to do this, but just off the top of my head, you could do the following:



1: Break up the words in Mail.txt to a list using the string library.
2: Break up the words in search to a list using the string library.
3: len (search)
4: Compare the lists and keep track of how many matches there are. If the # of matches = len(search), then you've found whatever you're looking for.
IF:
Comparison finds a match (Have a variable be set to 1 if it does):
Do Stuff

ELSE:

1:Whatever you want to happen, probably pass or something.

There's gotta be a pre-built function for something like this, but this method would also work, although it would be kinda slow and messy :/.

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 7:13 pm
by centip3de
insomaniacal wrote:There's gotta be a pre-built function for something like this, but this method would also work, although it would be kinda slow and messy :/.


Yea thanks for the help :) It probably would work, a bit too messy for my taste though. ;)

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 7:18 pm
by mojo1948
Depending on what your trying to do, something like this might work.
Code: Select all
text = 'anahsyTESTamjdsy'
search = 'TEST'
index = text.find(search)
print 'index = ',index
if index == -1:
   print 'FAIL'
else:
   print text[index:index+len(search)]
   print 'Do cool stuffz here'

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 7:33 pm
by centip3de
mojo1948 wrote:Depending on what your trying to do, something like this might work.
Code: Select all
text = 'anahsyTESTamjdsy'
search = 'TEST'
index = text.find(search)
print 'index = ',index
if index == -1:
   print 'FAIL'
else:
   print text[index:index+len(search)]
   print 'Do cool stuffz here'


I've seen something like this before, but I was unclear on what the -1 part of it meant. So I let it go hoping to find a way I understood..... o.O

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 7:57 pm
by mojo1948
The -1 is the value that is returned when the find function doesn't find a match. Return vales vary depending on what function you are calling. Try changing TEST to TAST and you will see what I mean.

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 9:44 pm
by Defience
I'd use regular expression as fashizzlepop suggested:

Code: Select all
import re

infile=open("Mail.txt","r")
lines=infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 9:50 pm
by centip3de
Defience wrote:I'd use regular expression as fashizzlepop suggested:

Code: Select all
import re

infile=open("Mail.txt","r")
lines=infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"


<3

***EDIT

Damn, I now have the error:

Traceback (most recent call last):
File "C:\Python26\POP3.py", line 15, in <module>
for line in lines:
NameError: name 'lines' is not defined


Current code:
Code: Select all
import getpass, poplib, sys, os, time, re

Mailbox = poplib.POP3_SSL("XXXX", 995)
Mailbox.user("XXXX")
Mailbox.pass_("XXXX")
messageCount = len(Mailbox.list() [1])
for i in range(messageCount):
      for j in Mailbox.retr(i+1) [1]:
            ff = open('Mail.txt', 'a')
            ff.write(j)
            ff.close()
                     
infile = open('Mail.txt', "r")
text = infile.readlines()
for line in lines:
    if re.search("test",line):
        print "Let's do coolz stuff here!"

Re: Noobish Python help pl0x

PostPosted: Mon Nov 29, 2010 10:42 pm
by tgoe
Re-read the post by Defience carefully