Keeping a function from taking to long--threads?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    Keeping a function from taking to long--threads?

    Hi All,
    Just wondering if anyone knows of a way to keep a function,
    E.g. socket.gethostb yaddr("12.34.56 .78"),[color=blue]
    >From taking to long-if it's run for more than 1 second, the function[/color]
    gethostbyaddr will be terminated?
    I was thinking about using threads, but I can't seem to get the hang of
    them.
    I've tried to have the script break if the clock time is greater than 1
    second, but python won't respond to anything until the function finishes,
    not surprisingly.
    Sorry if there is a very obvious answer to this...
    The below program is to do a search on our wireless network at work, so that
    we can find any rogue computers that aren't supposed to be there.

    Code:

    #code starts
    import socket
    import sys
    changingip=[]
    ip=sys.argv[1]
    ip=ip.split("." )
    for i in range(0,254):
    ipaddr=ip[0]+"."+ip[1]+"."+ip[2]+"."+str(i)
    try:
    #the next line is my issue; I need to make sure it doesn't go over 1 second
    while running
    print socket.gethostb yaddr(ipaddr)
    #the next block is to ensure that if the ip I am checking doesn't exist on
    the network, the script will move along to the next ip
    except:
    pass

    #end of script
    #I know the code isn't pretty, but except for the timeing issues, it works
    great.

    Any help is greatly appreciated.
    THX,
    Brandon McGinty

    --
    No virus found in this outgoing message.
    Checked by AVG Free Edition.
    Version: 7.1.385 / Virus Database: 268.2.5/284 - Release Date: 3/17/2006


  • Rene Pijlman

    #2
    Re: Keeping a function from taking to long--threads?

    <brandon.mcgint y@gmail.com>:[color=blue]
    >Just wondering if anyone knows of a way to keep a function,
    >E.g. socket.gethostb yaddr("12.34.56 .78"),[color=green]
    >>From taking to long-if it's run for more than 1 second, the function[/color]
    >gethostbyadd r will be terminated?[/color]

    import socket
    socket.setdefau lttimeout(1)

    --
    René Pijlman

    Wat wil jij leren? http://www.leren.nl

    Comment

    • Rene Pijlman

      #3
      Re: Keeping a function from taking to long--threads?

      <brandon.mcgint y@gmail.com>:[color=blue]
      >I'm using windows, and from what I've tried, the setdefaulttimeo ut
      >function doesn't work on my machine.[/color]

      It should. It does on mine.

      --
      René Pijlman

      Wat wil jij leren? http://www.leren.nl

      Comment

      • Steve Holden

        #4
        Re: Keeping a function from taking to long--threads?

        brandon.mcginty @gmail.com wrote:[color=blue]
        > Thanks, however, I forgot to mention that I'm using windows, and from what
        > I've tried, the setdefaulttimeo ut function doesn't work on my machine.
        > Again, thanks for your help!
        > Brandon McGinty
        >[/color]
        What version of Python are you running? Under 3.4 setdefaulttimeo ut()
        works perfectly well on my Windows system.

        If you are running 2.3 or earlier, you may want to use Timothy
        O'Malley's timeoutsocket module instead.

        However, you seem to be assuming that socket.gethostb yaddr() will report
        on the existence of systems, which I don't think it will - that function
        is for converting IP addresses to names:
        [color=blue][color=green][color=darkred]
        >>> import socket
        >>> socket.gethostb yaddr("127.0.0. 1")[/color][/color][/color]
        ('localhost', [], ['127.0.0.1'])[color=blue][color=green][color=darkred]
        >>> socket.gethostb yaddr("192.168. 10.215")[/color][/color][/color]
        ('bigboy.lan', [], ['192.168.10.215 '])[color=blue][color=green][color=darkred]
        >>> socket.gethostb yaddr("82.165.1 94.52")[/color][/color][/color]
        ('perfora.net', [], ['82.165.194.52'])[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        So it will tell you something about the DNS service's content rather
        than the existence or non-existence of systems at a particular IP address.

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd www.holdenweb.com
        Love me, love my blog holdenweb.blogs pot.com

        Comment

        Working...