so I am converting JSON data from a url into a string, then writing it to a text file. This is my current Python script (I'm using Python 2.7.6):
import json
import urllib
import time
startTime = time.time()
url = "http://someurl..."
success = False
while (True):
try:
txt = urllib.urlopen(url).read()
print " -> open URL time: %.3f" % (time.time() - startTime)
secondTime = time.time()
textFile = open('data.txt', 'w')
textFile.write("JSON Data (")
textFile.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
textFile.write("):\n")
textFile.write(txt)
textFile.close()
print " -> write file time: %.3f" % (time.time() - secondTime)
thirdTime = time.time()
success = True
break
except ValueError as valueErr:
print "Error:", err
except IOError as ioError:
print "Error: Internet connection issues."
break
if (success):
print " -> data.txt created()."
print " -> Finished."
print " -> Total Elapsed Time = %.3f" % (time.time() - startTime), "seconds."
else:
print " -> Finished."
and the output is as follows (I am running it in Windows command prompt, not the Python prompt):
'getCryptsyData.py' executing...
-> open URL time: 4.864
-> data.txt created().
-> write file time: 0.005
-> Finished.
-> Total Elapsed Time = 4.939 seconds.
My question is, is there any faster way of doing this? I.e. with a different python script or another scripting language or in C?
Edit 1: updated code and output to current script I am running.
json.loads()andjson.dumps()and just writingjsonURL.read()to the file?time curl http://someurl... > data.txtfrom a shell prompt. If that takes the same time, which it probably will, then the problem is your network and no code change will help. That command will also show a progress meter including things like average data transfer rate, which you can compare to the theoretical capacity of your LAN.curl: command not found" or equivalent, get it from here: curl.haxx.se Windows binaries are available if you scroll all the way to the bottom of their downloads page; I'd try the MSVC builds first (least likely to require you to install more stuff to make 'em work).)