Help?? Struct packing of Date time not has stopped working??!?!

quickbbs at my-deja.com quickbbs at my-deja.com
Tue Dec 14 10:16:07 EST 1999


Folks,

(Please if your going to email, please email replies to
junkster at rochester.rr.com, I don't check this Deja Account, and I'm just
using it due to work's firewall)

	A strange little problem has cropped up, that I can't explain.

My Python Time server is starting to cause errors, that I believe are
server related.

In other words, the client attachs, and reports: "struct.err: str size
does not match format".

Previous to (at least) 11/30/1999, it was/is working.  But we just
noticed that the client was reporting this error today (12/14/1999).

I suspect that STRUCT isn't handling the packing of the long correctly,
as far as the manual's go, it looks AOK, but.....

Here's the server code:

#
#       Python Time Server, based off of RFC 868.
#
#       Thanks to (no particular order, nor is this complete):
#
#               Jeff            [jam .at. quark.emich.edu]
#               Paul Foley      [(setq reply-to  (concatenate 'string
"Paul Foley "
#                                 "<mycroft" '(#\@) "actrix.gen.nz>"))]
#
#       And other folks in comp.language.python for their assistance with
this code.
#
#       This code is free to the public, EXCEPT, I would appreciate any
changes to be
#       emailed to me ("Junkster at rochester.rr.com")
#
# Changes:
#
#	v1.0a (10/15/99)
#		- Added Logging (see logfilename)
#		- Rewrote Time Generation routine to use time.time()
#		- Increased # of default threads
#

import time
from socket import *
import struct
import sys

#
#       Constants
#
epoch           = 2208988800L           # Difference between 1900 & 1970
per RFC868
logfilename     = 'TIMESERVER.LOG'      # Name of the log file
numb_threads    = 10


def timeserver_calculation():
	total = epoch + time.time()
	return total


def server(host="", port=37, backlog=5):
	sock = socket (AF_INET, SOCK_STREAM)
	sock.bind (host, port)
	sock.listen (backlog)
	print "listening on port %s (%s, %s)" % (port, `host`, backlog)
	while 1:
		connection = (conn, addr) = sock.accept()
		ft = timeserver_calculation()
		lt = long(ft)

		timetuple = time.localtime(time.time())
		logentry = time.strftime('%x %X', timetuple) + ' client ' +
str(addr[0]) + ' connected for timeStamp ('+str(lt)+')'

		print logentry
		logfile = open(logfilename, 'a')
		logfile.write (logentry+'\n')
		logfile.close



		conn.send ( struct.pack("!L", lt))
		conn.close()

if __name__ == "__main__":
	server('', 37, numb_threads)

----- Here's the client code ----


#       Python Time Server client, based off of RFC 868.
#
#       Thanks to the folks in comp.language.python for their assistance
with this code.
#
#       This code is free to the public, EXCEPT, I would appreciate any
changes to be
#       emailed to me ("Junkster at rochester.rr.com")
#

import  os
import  struct
import  sys
from    telnetlib       import Telnet
import  time

#
#       Use Telnet library to connect to server on port 37
#
print "Connecting to : ", sys.argv[1]
tn_session = Telnet(sys.argv[1], 37)    # Connect to NTP date / time
server
datetime   = tn_session.read_all()      # Read the Integer
tn_session.close()                      # Close the Telnet session

dt = struct.unpack ("!L", datetime)     # Unpack the data from the server

#
#       "Dump the Data to Screen for Debug"
#
print "Server Returned: ",datetime," |       Unpacked structure value :
", dt

#
#       Epoch is the diff. between Epochs between Python & RFC 868
#
epoch = 2208988800L
dt = (dt[0]) - epoch             # Remove "Tuple" status & Convert it to
a python time value

#
#       Print out "English" Version of time/date
#
timetuple = time.localtime(dt)
print "Time - ", time.asctime(timetuple)

#
#
#
if sys.platform == "win32":
        import win32api
        #print (timetuple[0], timetuple[1], timetuple[6], timetuple[2],
timetuple[3], timetuple[4], timetuple[5], 0)
        #win32api.SetSystemTime (timetuple[0], timetuple[1],
timetuple[6], timetuple[2], timetuple[3], timetuple[4], timetuple[5], 0)
        os.system ('date '+ time.strftime('%x', timetuple) )
        os.system ('time '+ time.strftime('%X', timetuple) )


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list