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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 3rd, 2004, 08:43 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 5 m 27 sec
Reputation Power: 5
check if file is running

greets

i am VERY new to python and have no prior programming experience.so please be patient if my queston seems trivial or impossible.ok,that said,i found a very cool script that i was able to make very minor modifications to,and make a exe out of with py2exe.basicly,what it does is start a game server,then monitors the server window for specific errors.

everything works great,but if the server were to lock or crash,then this script just ends.since its already watchin the server,is there a way i could have it try to restart the server and connect again if it locked/crashed?both the server.exe and the exe for this script are located locally in the same directory on a winxp machine.

Code:
#ServerCheck
#Copyright 2002 Richard Dillingham

from __future__ import division
import os, sys, os.path, string
import cPickle
import getpass
import telnetlib
import time

host = 'x.x.x.x'
port = xxxx
password = 'xxxxxx'
sendThis = 'xxxxxx'

os.system('start path\to\server.exe') #starts the server
time.sleep(5)

def main():
	sc = ElectricEye()
	sc.run()
class ElectricEye:
	def __init__(self):
		self.socket={}
		pass
	def run(self):
		while 1:
			tn = telnetlib.Telnet()
			tn.open(host,port)
			tn.write(' ')
			tn.write(password + "\n")
			while 1:
				data = tn.read_until('\n')
				if len(data)>0:
					strstart = "Setup_Start acct='"
					strend = "',"
					p = data.find(strstart)
					if p>-1:
						pe = data.find(strend,p+len(strstart))
						if pe>-1:
							col1=data.find(':')
							if col1>-1:
								col2=data.find(':',col1+1)
								if col2>-1:
									col3=data.find(':',col2+1)
									if col3>-1:
										sockNum=data[col2+1:col3]
										acctname=data[p+len(strstart):pe]
										self.socket[sockNum]=acctname
										print 'memorizing accountname='+acctname+' sockNum='+sockNum
					try:
						strstart = ":ERROR:"
						strend = ":Bad Msg" #00 Eat 4 Bytes"
						p = data.find(strstart)
						if p>-1:
							pe = data.find(strend,p+len(strstart))
							if pe>-1:
								sockNum=data[p+len(strstart):pe]
								acctname=self.socket[sockNum]
								print '********************'
								print 'ERROR by accountname='+acctname+' sockNum='+sockNum
								#get uid
								tn.write('account '+acctname+' show lastcharuid\n')
						strstart = "'lastcharuid' for '"
						strend = "' is '"
						p = data.find(strstart)
						if p>-1:
							pe = data.find(strend,p+len(strstart))
							if pe>-1:
								acctname=data[p+len(strstart):pe]
								val=data[pe+len(strend):]
								quotepos = val.find("'")
								val=val[:quotepos]
								print acctname+' has been disconnected and blocked.'
								tn.write('uid.'+val+'.sysmessage '+sendThis+'\n')
								
								#time.sleep(3) #pause for 3 seconds.
								
								tn.write('uid.'+val+'.kick\n')
								tn.write ('b ElectricEye blocked account "'+acctname+'" for using an illegal client!\n')
								#get last ip
								tn.write('/account update\n')
								tn.write('account '+acctname+' show lastip\n')
						strstart = "'lastip' for '"
						strend = "' is '"
						p = data.find(strstart)
						if p>-1:
                                                        pe = data.find(strend,p+len(strstart))
                                                        if pe>-1:
                                                                 acctname=data[p+len(strstart):pe]
                                                                 val=data[pe+len(strend):]
                                                                 quotepos = val.find("'")
                                                                 val=val[:quotepos]
                                                                 print 'the IP '+acctname+' used was '+val
								 print '********************'
								#change disconnect to remove 1 if you want to delete their character too.
								#Disconnect won't work in older versions, but you can send a bad gump msg.
							#22:19:170:Setup_Start acct='syrius', char='Syrius'
							#22:19:ERROR:170:Bad Msg 00 Eat 4 bytes, prv=02
					except KeyError:
						pass
					
		tn.close()
		


if __name__ == '__main__': main()

__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

Reply With Quote
  #2  
Old January 4th, 2004, 12:05 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Hi there. I might have an answer for you. It's quite simple in a way.

Simple replace your main by something like this :
Code:
def main():
   while True:
      try:
         sc = ElectricEye()
         sc.run()
       except:
         file = open('somefile.txt','w') #You should change its name ;)
         file.write("Exception occured.")
         file.write("Type : " + sys.exc_info()[0])
         file.write("Value : " + sys.exc_info()[1])
         file.write("Traceback : " + sys.exc_info()[2])
         file.close()


BEWARE though ! There HAS to be a simpler way : I'm internationally known for using the most complicated of ways to solve problems. Anyway, just try it : it'll put valuable info in text file about the error that occured and then just loop back and restart the server. Good luck !

Reply With Quote
  #3  
Old January 4th, 2004, 09:30 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,529 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 17 h 19 m 5 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
Like your solusion Solar, kinda a built-in error log . Anyway as long as the server doesn't exit with an error, all you really need is the while loop! Which you can put inside or outside the main() function i.e.

Code:
if __name__ == '__main__':
	while True: main()


or

Code:
def main():
	while True:
		sc = ElectricEye()
		sc.run()


do exactly the same thing but one is internal to the function and the other isnt . Now that i think about it you can also stick the loop inside our class if you wanted to .

As for actually being able to check if the server class is running you could have a variable which stores the servers status as part of the class? Seems to be the best way to "check if file is running" IMO

The class (and i'm not saying this to have a go at you or anyone else for that matter) just has so many design flaws and there are countless ways to improve the script . Sorry to say it but your probably better of ripping the whole thing down and starting over

Take care guys,
Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #4  
Old January 7th, 2004, 02:12 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 5 m 27 sec
Reputation Power: 5
sorry it took so long for me to reply,but thank you for the help guys!and SoalrBears's error log is very cool!

but unfortunately like netytan said,the script is flawed.it seems the first loop for the telnet loops infinately.so therefore while the script monitors the server,it causes a error which makes it shut down when the server does

so it looks like i'll take netytans advise and try to start over.and no worries netytan,i am not offended.infact,i appreciate your honesty!

just wanted to say thx for the help guys...and keep it up!your efforts are appreciated!

cheers!

Reply With Quote
  #5  
Old January 7th, 2004, 03:02 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 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 17 h 19 m 5 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
No problem B, and good luck with the project! If you post your code when your done i'd be very interested to see what you come up with .

A few little tips:

don't use the string modul; i have nothing against it but i see no reason to use it since you can do everything you can with the string module using the string type methods...

make sure whatever you do import gets used, and for a good reason! i.e. you're not using __future__ devision on there at all

Think carefully about your class design if your going to use OOP, the ElectricEye class (aswell as having a very cryptic name) could just as easily (and more efficently) been a function!

We'll hope this helps,

take care,
Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > check if file is running


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 5 hosted by Hostway