Python TimeServer (RFC 868)
Benjamin Schollnick
junkster at nospam.rochester.rr.com
Wed Sep 29 21:57:13 EDT 1999
Jeff,
Here's the code I'm currently using:
import time
#
#
#
epoch = 18000.0 # Thu Jan 01 @ 00:00:00 @ 1970 (Win NT v4 &
v1.5.2-0)
tserver_base = 2208988800.0
def timeserver_calculation():
temp = tserver_base + epoch + time.time()
return temp
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()
# <<<<<<<<<<<<<<<<<<<
conn.send ( ft )
# <<<<<<<<<<<<<<<<<<<
conn.close()
if __name__ == "__main__":
server('', 37, 1)
If I change the marked lines to:
ft = float (timeserver_calculation() )
After all, RFC 868 says "send the time as a 32bit binary number".
C:\InetPub\wwwroot\TIMESE~1>test.py
listening on port 37 ('', 1)
connected by <socket._socketobject instance at 7f4d60>
at ('13.175.214.61', 4153)
Traceback (innermost last):
File "C:\InetPub\wwwroot\TIMESE~1\TEST.PY", line 31, in ?
server('', 37, 1)
File "C:\InetPub\wwwroot\TIMESE~1\TEST.PY", line 27, in server
conn.send ( float(ft))
File "<string>", line 1, in send
TypeError: argument 1: expected read-only buffer, float found
Evidently conn.send doesn't like anything other than "ascii" /
Strings.
Does anyone have a method to send binary data via socketserver?
- Benjamin
More information about the Python-list
mailing list