0

I want to do the following:

requestor = UrlRequestor("http://www.myurl.com/question?timestamp=", {'Content-Type': 'application/x-www-form-urlencoded', 'Cookie' : self.EASW_KEY + ";", 'X-UT-SID' :self.XUT_SID}, 'answer=' + self.securityHash)
requestor.open()
self.FUTPHISHING = requestor.getHeader('Set-Cookie').split(';')[0]

Right after timestamp, I would like to have the local time in this format: 1355002344943

How can I do this?

David Robinson
  • 77,383
  • 16
  • 167
  • 187
user203558
  • 831
  • 1
  • 6
  • 5

2 Answers2

1

You can get the time in that format from the time module. Specifically I'd do it this way

import time

timeurl = "http://www.myurl.com/question?timestamp=%s" % time.time()
requestor = UrlRequestor(timeurl, {'Content-Type': 'application/x-www-form-urlencoded',      'Cookie' : self.EASW_KEY + ";", 'X-UT-SID' :self.XUT_SID}, 'answer=' + self.securityHash)
requestor.open()
self.FUTPHISHING = requestor.getHeader('Set-Cookie').split(';')[0]

time.time() returns a float, so if it doesn't like that level of precision you can do

timeurl = "http://www.myurl.com/question?timestamp=%s" % int(time.time())
oathead
  • 452
  • 2
  • 5
  • Thank you very much, but is the $s required? – user203558 Dec 08 '12 at 22:02
  • 1
    You could also do `"http://www.myurl.com/question?timestamp={}".format(time.time())` which is the new way of string formatting in Python -- these are the two ways to do this, however. – Sam Mussmann Dec 08 '12 at 22:03
  • Sam makes a good point, the .format() is the preferred way. There's a good discussion of it [here](http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) – oathead Dec 08 '12 at 22:13
  • Thanks guys it was really helpful, but do I really need to put timeurl = before I mean to creat a new variable? – user203558 Dec 08 '12 at 22:14
  • @user203558: Separate variables for the timestamp or the url are not necessary, but they can help reduce the `requestor` line's length. In general it's a good practice to avoid lines that can't all fit on your editor's screen at once. – Blckknght Dec 08 '12 at 22:19
1

That timestamp looks to be based off of Unix time (e.g. seconds since Jan 1 1970), but it has three more digits. Probably it is in milliseconds, not seconds. To replicate it, I suggest doing:

import time

timestamp = int(time.time()*1000)
url = "http://www.myurl.com/question?timestamp=%d" % timestamp

You could also simply concatenate the timestamp onto the URL, if you don't want to do string formatting:

url = "http://www.myurl.com/question?timestamp=" + str(timestamp)
Blckknght
  • 100,903
  • 11
  • 120
  • 169