Jump to content

I Need Some Python Help...


lukeh53

Recommended Posts

I am working on a science fair project and i am getting frustrated with python. I have two programs, one that receives information from the network that was sent by the other. the one that sends the information is sending the system time, which i got from time.time(), and the second takes that time, and subtracts it from the current time to find how long it took for the data to travel. what frustrates me is the fact that i am getting negative numbers on some runs. here is the code for the programs:

this one receives the time:

#!/usr/bin/python

# Filename: time-server.py

import socket, sys, time

HOST = ''"
PORT = int(sys.argv[1])

s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print '[+] Connection from: ', addr

while 1:
	data = float(conn.recv(1024))
	if not data: break
	print '\n[+] Time to send: ', time.time() - data, "\n"
conn.close()

and this one sends the time:

#!/usr/bin/python

# Filename: python-client.py

import socket, sys, time

HOST = 'localhost'
PORT = int(sys.argv[1])

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(str(time.time()))
s.close()

print '\n[+] Finished\n'

any help or direction would be greatly appreciated.

Link to comment
Share on other sites

The system clocks will not be perfectly in sync. Your best bet to have them synchronise perfectly would be to run a NTP server on one of them and have the other computer get the time from there.

sorry, i forgot to mention that i am running both programs on the same machine right now. i didn't want to mess with different systems until i knew the code worked.

Link to comment
Share on other sites

lol, I should have seen that from the bit that says host = localhost.

I don't think there is any thing wrong with the scripts with regards to the code used or rather it's logic. Since python is an interpreted language it may be doing some sneak things to mess with it.

Try assigning the time to a variable before printing and subtracting.

It may also be some thing silly like padded zeros sneaking in and messing with the number meaning. Try printing the time before it's subtracted to see what the number actually is.

Link to comment
Share on other sites

I did as you suggested, but still no luck. heres the new output:

from the server:

[+] Connection from: ('127.0.0.1', 48697)

Time Now: 1270257838.17

[+] Time to send: -0.00209617614746

and from the client:

Time Now: 1270257838.17

[+] Finished

Link to comment
Share on other sites

The problem is that str only shows the first two decimal places. There is probably a better way to rectify this, but you can convert the float to hex (using the objects hex method) then back to a float from the hex value with float.fromhex. In this way nothing is lost.

Link to comment
Share on other sites

Well, all was working fine until windows had something to say. up to now i have been running only on ubuntu, but i need to have some windows hosts as well. and naturally windows doesnt agree with my code. apparently the float.hex() and float.fromhex() functions dont exist in windows, so im back to negative times.

Some details:

Python 2.5

Windows Vista

Heres the updated server code:

#!/usr/bin/python
# Filename: time-server.py
# author: luk53
# Help provided by: Sparda

import socket, sys, time

HOST = ''
PORT = int(sys.argv[1])

s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print '[+] Connection from: ', addr

while 1:
	data = float.fromhex(conn.recv(1024))  #This line is causing problems
	if not data: break
	print '\nTime Now: ', time.time(), '\n[+] Time to send: ', time.time() - data, "\n"
conn.close()

and the client:

#!/usr/bin/python
# Filename: python-client.py
# author: luk53
# Help provided by: Sparda

import socket, sys, time

HOST = 'localhost'
PORT = int(sys.argv[1])

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
timeNow = time.time().hex() #this line is causing problems
print '\n Time Now: ', timeNow
s.send(str(timeNow))
s.close()
print '\n[+] Finished\n'

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...