初めての
■初めての python script
■test.py
#!/usr/bin/python import thermosensor t=thermosensor.thermosensor() for key in t.show_keys_array(): print key + " : " + t.status(key)
■thermosensor.py
#!/usr/bin/python import urllib2 import sys class thermosensor: """This is a thermosensor class""" def __init__(self): """This is a init""" self.url='http://192.168.1.200/B/crrntdata/cdata.txt' self.dic={} try: self.dh=urllib2.urlopen(self.url) for line in self.dh.readlines(): if(line.find('=')<0): continue (a,b)=line.rstrip().split('=',1) self.dic[a]=b except urllib2.URLError: print "Error on communication with thermosensor was occured." raise except: raise def status(self,arg): """This method returns a value specified with arg""" if(self.dic.has_key(arg)): return self.dic[arg] else: print "no such key in thermosensor" return -1 def status_all(self): """This method prints all items""" print "Status: " for key, value in self.dic.iteritems(): print "\t" + key + " = " + value return 1 def show_keys_array(self): keys=[] for key, value in self.dic.iteritems(): keys.append(key) keys.sort() return keys if __name__=="__main__": a=thermosensor() print a.__doc__ print dir(thermosensor) print a.__init__.__doc__ print a.status('Time') + a.status('cTemperature1') + a.status('cHumidity') a.status("temp") a.status_all() print a.show_keys_array()