Python: Time of day to execute
<p>I have written a small script to fetch instant stock prices.</p>

<pre><code>#script to get stock data

from __future__ import print_function
import urllib
import lxml.html
from datetime import datetime
import sys
import time

stocks=["stock1","stock2","stock3","stock4","stock5"]

while True:
 f=open('./out.txt', 'a+')
 for x in stock:
  url = "http://someurltofetchdata/"+x
  code   = urllib.urlopen(url).read()
  html   = lxml.html.fromstring(code)
  result = html.xpath('//td[@class="LastValue"][position() = 1]')
  result = [el.text_content() for el in result]
  f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ' ' + x + ' ' + result[0])
  f.write("\n")
 f.close()
</code></pre>

<p>I want that code to fetch data only on hours the stock market is open which means on trading hours. (09:00 to 12:30 and 13:30 to 17:30).</p>

<p>Could you please suggest a method to perform the scheduling implicitly on the code? (Not on the OS level)</p>
