Looping a script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • T00l
    New Member
    • Jul 2007
    • 16

    Looping a script

    Hi

    I have the following script that counts the amount of words in a file, prints an output, closes the file and then waits for 10secs. I need the script to repeat itself after it was waited the 10secs, anyone know how I can do this?

    import time
    text = open("c:\\file. txt").read()
    v = text.count('wor d')
    if v > 2:
    print "has words"
    text.close()
    time.sleep(10)

    Thanks
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by T00l
    Hi

    I have the following script that counts the amount of words in a file, prints an output, closes the file and then waits for 10secs. I need the script to repeat itself after it was waited the 10secs, anyone know how I can do this?

    import time
    text = open("c:\\file. txt").read()
    v = text.count('wor d')
    if v > 2:
    print "has words"
    text.close()
    time.sleep(10)

    Thanks
    Are you familiar with loops? Check some tutorials online if not.:
    [code=python]
    while 1:
    text = open("c:\\file. txt").read()
    v = text.count('wor d')
    if v > 2:
    print "has words"
    text.close()
    time.sleep(10)
    [/code]

    Comment

    • T00l
      New Member
      • Jul 2007
      • 16

      #3
      Hi

      Thanks for the reply, i'm learning python/programming from a couple of books but couldn't see that anywhere, however after your post and looking in the books again, its staring me in the face!

      Now that I have that loop going, I want to add it to an existing looping script. However the first loop is continuous so the program does not seem to be executing my second loop code, can you tell me if there is a better loop than the p.loop for the first section of code to allow it (While still looping) to continue and execute the second loop part of code?

      import sys
      import pcapy
      import time
      import time
      from impacket.Impact Decoder import *

      def recv_pkts(hdr, data):
      While 1:
      x = EthDecoder().de code(data)
      sys.stdout = open('c:\\dir\\ file.txt', 'a')
      print x


      def get_int():
      devs = pcapy.findallde vs()
      i=0
      for eth in devs:
      print " %d - %s" %(i,devs[i])
      i+=1
      sel=input(" Select interface: ")
      dev=devs[sel]
      return dev

      dev = get_int()
      p = pcapy.open_live (dev, 1500, 0, 100)
      p.setfilter('tc p')
      print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
      p.loop(-1, recv_pkts)

      while 1:
      text = open("c:\\dir\\ file.txt").read ()
      v = text.count('wor d')
      if v > 2:
      print "has words"
      text.close()
      time.sleep(10)

      Comment

      • T00l
        New Member
        • Jul 2007
        • 16

        #4
        Sorry, the while: 1 on line 8 should not be there, the function is

        def recv_pkts(hdr, data):
        x = EthDecoder().de code(data)
        sys.stdout = open('c:\\dir\\ file.txt', 'a')
        print x

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by T00l
          Hi

          Thanks for the reply, i'm learning python/programming from a couple of books but couldn't see that anywhere, however after your post and looking in the books again, its staring me in the face!

          Now that I have that loop going, I want to add it to an existing looping script. However the first loop is continuous so the program does not seem to be executing my second loop code, can you tell me if there is a better loop than the p.loop for the first section of code to allow it (While still looping) to continue and execute the second loop part of code?

          import sys
          import pcapy
          import time
          import time
          from impacket.Impact Decoder import *

          def recv_pkts(hdr, data):
          x = EthDecoder().de code(data)
          sys.stdout = open('c:\\dir\\ file.txt', 'a')
          print x


          def get_int():
          devs = pcapy.findallde vs()
          i=0
          for eth in devs:
          print " %d - %s" %(i,devs[i])
          i+=1
          sel=input(" Select interface: ")
          dev=devs[sel]
          return dev

          dev = get_int()
          p = pcapy.open_live (dev, 1500, 0, 100)
          p.setfilter('tc p')
          print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
          p.loop(-1, recv_pkts)

          while 1:
          text = open("c:\\dir\\ file.txt").read ()
          v = text.count('wor d')
          if v > 2:
          print "has words"
          text.close()
          time.sleep(10)
          I am not familiar with the pcapy module but you could try using threads:
          [code=python]
          import thread
          def wordcount():
          while 1:
          text = open("c:\\dir\\ file.txt").read ()
          v = text.count('wor d')
          if v > 2:
          print "has words"
          text.close()
          time.sleep(10)

          def recv_pkts(hdr, data):
          x = EthDecoder().de code(data)
          sys.stdout = open('c:\\dir\\ file.txt', 'a')
          print x


          def get_int():
          devs = pcapy.findallde vs()
          for (i, eth) in enumerate(devs) : # no need for extra variable
          print " %d - %s" %(i,devs[i])

          sel=input(" Select interface: ")
          dev=devs[sel]
          return dev

          dev = get_int()
          p = pcapy.open_live (dev, 1500, 0, 100)
          p.setfilter('tc p')
          print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
          thread.start_ne w_thread(wordco unt, ())
          p.loop(-1, recv_pkts)
          [/code]

          Comment

          • T00l
            New Member
            • Jul 2007
            • 16

            #6
            Hi

            Thanks for that, I haven't looked at threading before but will look into it further now.
            I think the problem i'll have is that the def recv_pkts needs to run continually where as the def wordcount() needs to only run every 10 secs, it looks from the modiftication you have done the whole script will wait 10 secs?
            Is this correct or have I got that wrong?

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #7
              Originally posted by T00l
              Hi

              Thanks for that, I haven't looked at threading before but will look into it further now.
              I think the problem i'll have is that the def recv_pkts needs to run continually where as the def wordcount() needs to only run every 10 secs, it looks from the modiftication you have done the whole script will wait 10 secs?
              Is this correct or have I got that wrong?
              Nope, that's the thing about threads, they run in parallel. The time.sleep call only works for its thread so the main script will keep on running and only the thread will sleep.

              There is also a threading module that you can also use. Make sure to check the documentation for both modules to get a better understanding.

              Comment

              Working...