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

Fredrik Lundh fredrik at pythonware.com
Thu Dec 16 07:48:36 EST 1999


quickbbs at my-deja.com wrote:
> I suspect that STRUCT isn't handling the packing of the long correctly,
> as far as the manual's go, it looks AOK, but.....

well, telnet is not a binary protocol, and using
"!L" to unpack a 32-bit integer is a lousy idea --
what if you're running on a 64-bit platform?

try this one instead:

# socket-example-1.py (from the eff-bot guide)

import socket
import time

# server
HOST = "www.python.org"
PORT = 37

# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00

# connect to server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

# read 4 bytes, and convert to time value
t = s.recv(4)
t = ord(t[3]) + (ord(t[2])<<8) + (ord(t[1])<<16) + (ord(t[0])<<24L)
t = int(t - TIME1970)

s.close()

# print results
print "server time is", time.ctime(t)
print "local clock is", int(time.time()) - t, "seconds off"

## sample output:
## server time is Sat Oct 09 16:42:36 1999
## local clock is 8 seconds off

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list