TimeServer & Clients updated
Benjamin Schollnick
junkster at nospam.rochester.rr.com
Sun Oct 17 20:59:34 EDT 1999
Here's the current code base for v1.0a of TimeServer & Timeclients.
Here's a few notes:
1) The code is primarily (currently) aimed at Python on a Win NT
platform. After all, this was spec'd to be used at work, which
is a Win NT environment.
* Any cross platform suggestions, improvements are more
than welcome.
2) Any suggestions on additional error checking, sanity checks, etc
are more than welcome.
3) Any suggestions for general improvements or anything else, is
certainly welcome.
4) If anyone wants to archive this code, feel free, but please leave
my comments/headers/acknowledgement intact.
This also applies to Python releases, if this happens to
get attached to the code tree, I won't shed any tears... |-)
5) I'm also trying to come up with a "plugin" Scheme, since I plan to
make other "servers", but I'm coming up dry on ideas. Anyone
want to "spare a idea" or two?
Acknowledgements:
Everyone that has contributed directly or indirectly in helping keep
my head attached straightly.
Improvement/Change history:
* First release official release
====================================================
Port 37, Timeserver 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)
====================================================
Client 37
====================================================
# 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) )
====================================================
====================================================
Client 13
====================================================
#
# 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')
#timestr = time.strptime (datetime)
#print timevalue, ' - ', time.asctime(timevalue)
print
====================================================
(Remove "NoSpam" to Email me)
====================================================
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