Puzzled with Time Server conversion problems?

Benjamin Schollnick junkster at nospam.rochester.rr.com
Mon Oct 4 21:37:02 EDT 1999


Here's a follow up.

Finished TimeServer code, with "Clients" for port 37 (NTP), and for 
Port 13 (Datetime).

Please note these clients are *NOT* finished, because I don't see 
anyway to set the
date & time inside of PYTHON.  Anyone that can point out a way to 
change the computers
clock via python, I'll appreciate it.

Also, as far as I can tell, the code is AOK, and returns the same 
values as RFC 868 via
telneting & using the client37.py code.  But I've had little luck with
verifying this via
NT time client daemon.  

Can anyone assist in trying this server out with *KNOWN* TCP NTP 
clients?  

Client13 was written to take advantage of a daemon running under 
Windows NT on
port 13, which broadcasts like this:

		Mon Oct  4 21:27:45 1999 (In plain text.)

Which is nice, but annoying because for some *STRANGE* reason I can't 
get 
time.strptime (datetime, '%A, %B, %d, %Y %X') to work.

Everytime I try STRPTIME, I get a attribute error.  Can anyone point 
out what 
I'm goofing up?  

BTW-> Anyone have suggestions for making the server a little bit 
"friendlier" either
	code / execution wise?

----- Server ----
#
#	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")
#
import time
import struct

#
#       Leapyear (year): Returns 0 if not leapyear, 1 if leapyear
#
def leapyear(year):
        temp = ((year % 4 == 0) and (year % 100 <>0)) or (year % 400 
== 0)
        return temp


#
#       calc_leap_years(year):  Returns the # of years between 1900 & 
year
#                               that are leapyears. (i.e. 5, etc)
#
def calc_leap_years (year):
        temp = 0
        if year > 1900:
                for count in range(1900, year):
                        if leapyear(count):
                                temp = temp + 1
        return temp

def timeserver_calculation():

        secs_per_day    = 24.0*60*60    # 86400
        secs_per_year   = 365.0 * secs_per_day
        leapyear        = 1896          # (1880, 1884, 1888, 1892, 
1896, 1904, 1908)

        timetuple       = time.gmtime(time.time())

        total           = secs_per_year *  (timetuple[0]-1900)
        total           = total + (calc_leap_years(timetuple[0]) * 
secs_per_day)
        total           = total + (timetuple[7] - 1) * secs_per_day
        total           = total + timetuple[5] + timetuple[4]*60.0 + 
timetuple[3]*3600.0
        print total
        return total

import sys

from socket import *

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()
                print 'connected by %s \nat %s' % connection
                ft = timeserver_calculation()
                lt = long(ft)
                conn.send ( struct.pack("!L", lt))
                conn.close()

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

---------- Client37 -------------------
#
#	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 time
import struct
import sys
from telnetlib import Telnet

print "Connecting to : ", sys.argv[1]
tn_session = Telnet(sys.argv[1], 37) # Connect to MS Win NT date / 
time
datetime   = tn_session.read_all()
tn_session.close()
print "Server Returned: ",datetime
dt = struct.unpack ("!L", datetime)
print "Unpacked structure value : ", dt
#timevalue  = time.strptime (datetime, '%A, %B, %d, %Y %X')
#timevalue  = time.strptime (datetime)
#print timevalue, '   - ', time.asctime(timevalue)
print

------------ Client13.py -----------
#
#	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 time
import sys
from telnetlib import Telnet

print "Connecting to : ", sys.argv[1]
tn_session = Telnet(sys.argv[1], 13) # Connect to MS Win NT date / 
time
datetime   = tn_session.read_all()
tn_session.close()
print "Server returned: ",datetime
#timevalue  = time.strptime (datetime, '%A, %B, %d, %Y %X')
#timevalue  = time.strptime (datetime)
#print timevalue, '   - ', time.asctime(timevalue)
print 

--------------------------------------
================================
Please feel free to copy any and or
all of this sig.
A little something for spam bots:

root at localhost postmaster at localhost admin at localhost
abuse at localhost postmaster at 127.0.0.1

Chairman William Kennard: bkennard at fcc.gov 
Commissioner Susan Ness: sness at fcc.gov
Commissioner Harold Furchtgott-Roth: hfurchtg at fcc.gov
Commissioner Michael Powell: mpowell at fcc.gov
Commissioner Gloria Tristani: gtristan at fcc.gov
consumerline at ftc.gov
fccinfo at fcc.gov
ssegal at fcc.gov





More information about the Python-list mailing list