Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2004, 03:48 AM
EJP EJP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 EJP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 9 sec
Reputation Power: 0
Problems looping

This is the first program I have started to write, and am getting hung up on a few things..

All I'm trying to do is have the system alert the system bell a few times once a connection is made to my computer by a certain IP and port number, and then sound the bell only once or twice once the connection goes away. Then it will have to loop back to the beginning to watch for a connection to be made, and go away and so on till I kill it. I am only at the very first part of this so far.



Code:
import os
import time

o=os.popen("netstat -an")

connected = "no"

while connected != "yes":
	for l in o:
		try:
			if l.split()[1].endswith("192.168.0.250:21"):
				print "\a\a\a\a\aMatch!"
				connected = "yes"
			else:
				print "Nothing"	
		except IndexError:
			print "Index Exception"
	time.sleep(1)



Now the problem is this. It will only find a match if there is a established connection before I start the program. So lets say there is no current ftp connection, and I start the program it seems to be running however once I do make a connection It never will sound the bell..

So It seems to be only going though the for loop once.

Thanks for any help.

Reply With Quote
  #2  
Old October 6th, 2004, 04:31 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Hi EJP,
You need to put your code like this:
Code:
while ....
    o=os.popen("netstat -an")
    for l in o....
        ......


Have fun

BTW it is usually a good idea to avoid single letter variable names like o and l as they look very much like 0O and 1|.

grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old October 6th, 2004, 08:15 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You should also use the boolean values True or False; you're currently using two strings (yes and no) i.e.

Code:
connected = False

while not connected:
    ...do whatever...


Note: The boolean values True and False can also be expressed as 1 and 0 respectivly. If you're running a version older than Python 2.3 then you will need to use these rather numbers. It might also be handy to know that an empty object (string, list, tuple or dictionary) are also treated as being False.

Have fun,

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : October 6th, 2004 at 08:20 AM.

Reply With Quote
  #4  
Old October 11th, 2004, 01:59 AM
EJP EJP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 EJP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 9 sec
Reputation Power: 0
Thanks for the help.


After putting this...

Code:
o=os.popen("netstat -an")


After the while statement got it looping the way I wanted it to. Now I just need to figure out the rest of it out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problems looping


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT