I have 3 lat/lngs and a URL that I am constructing. My output should be 3 URLs, for each lat/lng, I am receiving 6. What do I need to change in my code below to print 3 URLs instead of 6? The try block and first for loops start are error handling, if the script fails, try twice. I am getting 6 values even when the script does not fail.
def main():
for i in range(2):
for attempts in range (1):
try:
for lat, lon, id_, startDate, endDate in zip(latval, lonval, idVal, startDayValStr, endDayValStr):
time_param = '?start='+ startDate +'T'+ "01:00" + 'Z' + '&end='+ endDate + 'T' + "23:00" + 'Z'
hrPrecip = 'https://insight.api.wdtinc.com/hourly-precipitation/' + str(lat)+'/' + str(lon) + time_param + '&unit=inches'
print hrPrecip
except Exception as e:
attempts = i + 1
sleep(30)
print "now trying attempt #" + " " + str(attempts) + " " + "for error" " " + str(e)
print(traceback.format_exc())
logging.exception(e)
msg = "PYTHON ERRORS:\nTraceback info:\n" + traceback.format_exc()
logging.debug("sending error email")
emailserver.email_error_msg(msg)
if __name__ == "__main__":
main()
Output:
https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.797207/-95.175648?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.796302/-95.180946?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches
https://insight.api.wdtinc.com/hourly-precipitation/44.778728/-95.23022?start=2019-05-13T01:00Z&end=2019-05-13T23:00Z&unit=inches`
It could be the try: and except: block. failing in the first. I am guessing you do not need the second loop with attempt in range(1). In fact you do not need any loop here.
Related
My code:
elif message.content.startswith(";random-number"):
channel = client.get_channel(1004728545911787740)
await channel.send("**Command received:** " + str(message.content) + "\n ```" + str(message) + "```")
try:
range = int(message.content.replace(";random-number ", "")
await message.channel.send("Random number: " + random.randint(0, int(range)))
except:
await message.channel.send(random.choice(["Use the command like this: ", "Here is how you can use the command: "]))
await message.channel.send("`;random-number [max number]`\nExample:\nUser - `;random-number 100`\nCorion - Random number: 67")
When I run my code I get this error:
File "main.py", line 150
await message.channel.send("Random number: " + random.randint(0, int(range)))
^
SyntaxError: invalid syntax
It seems there a syntax problem at this line:
I can't really understand where the syntax error is though. I also tried str-ing the random.randint(0,int(range) but it doesn't work either. Any help is appreciated!
Your syntax issue is the line above
range = int(message.content.replace(";random-number ", "")
You are missing the closing brace
range = int(message.content.replace(";random-number ", ""))
I had an Python script that running continuously. If there any new file on directory then the python will open url using urllib2 to do some request on specific ip.
here are the code
encoded_string = base64.b64encode(image_file.read())
values = dumps({
'image_data':encoded_string,
'requestCode':'111'
})
headers = {"Content-Type": "application/json"}
request = Request("http:/xx.xxx.xx.xxx/api/carplate_service",data=values, headers=headers)
response = urlopen(request, timeout=60)
The code are working well but on random time, let say usually happened on 1-2 AM then I got this error:
<class 'urllib2.URLError'> - <urlopen error [Errno 110] Connection timed out>
I had an exception on that function on this bellow :
try:
ip = sys.argv[1]
histId = int(sys.argv[2])
handler = ModHandler()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('./' + ip + '/', pyinotify.IN_CLOSE_WRITE)
notifier.loop()
except BaseException as e:
with open("error.log", "a") as text_file:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
text_file.write( time.strftime("%Y-%m-%d %H:%M:%S") + " [" + str(exc_tb.tb_lineno) + " - " + fname + "] : " + str(exc_type) + " - " + str(e) + "\n")
text_file.close();
The exception not working well because application cannot continue if there are some error like above.
My question are how to make program still continue even the exception throw?
I'm using python2.6
Thanks
For function calls that go out to external services I usually find that the following basic structure works pretty well
import time
expire_time = 2
while True:
start_time = time.time()
try:
# Do something here
# ....
# If you make it to the bottom of the code, break out of the loop
break
except BaseException as e:
# Compare the start_time with the current time
now_time = time.time()
if now_time > start_time + expire_time:
raise e
# Otherwise try executing the `try` block again
Using the code that you've provided it could look something like this
import time
expire_time = 2
while True:
start_time = time.time()
try:
ip = sys.argv[1]
histId = int(sys.argv[2])
handler = ModHandler()
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('./' + ip + '/', pyinotify.IN_CLOSE_WRITE)
notifier.loop()
break
except BaseException as e:
now_time = time.time()
if now_time > start_time + expire_time:
raise e
else:
with open("error.log", "a") as text_file:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
text_file.write( time.strftime("%Y-%m-%d %H:%M:%S") + " [" + str(exc_tb.tb_lineno) + " - " + fname + "] : " + str(exc_type) + " - " + str(e) + "\n")
text_file.close();
After multiple attempts, this code still fails. What I am trying to do is send "cpu stats" as JSON over to the server. The thing is, cpustats alone is fine - there is just 1 namedtuple with the different cpupercentages - user, idle, etc.. But 'percpu' returns a list of each cpu's namedtuple (of user, idle, etc.). So I cannot convert the list to a dictionary. I am trying to loop through the list and then send each one of the namedtuple to the server. (For ref. - I am using 2.7.5). The script worked fine without the attempt to loop and try/except - it returned a '200 OK'. But now when I run it it doesn't even return an error, any response message/status. It's as if the script is just bypassing the whole try/except block. Just the 'print cpuStats' line at the end delivers as it should. (The indentation in this question is a bit off but it's fine in the script)
import psutil
import socket
import time
import sample
import json
import httplib
import urllib
serverHost = sample.host
port = sample.port
thisClient = socket.gethostname()
currentTime = int(time.time())
s = socket.socket()
s.connect((serverHost,port))
cpuStats = psutil.cpu_times_percent(percpu=True)
print cpuStats
def loop_thru_cpus():
for i in cpuStats:
cpuStats = cpuStats[i]
cpuStats = json.dumps(cpuStats._asdict())
try:
command = 'put cpu.usr ' + str(currentTime) + " " + str(cpuStats[0]) + "host ="+ thisClient+ "/n"
s.sendall(command)
command = 'put cpu.nice ' + str(currentTime) + " " + str(cpuStats[1]) + "host ="+ thisClient+ "/n"
s.sendall(command)
command = 'put cpu.sys ' + str(currentTime) + " " + str(cpuStats[2]) + "host ="+ thisClient+ "/n"
s.sendall(command)
command = 'put cpu.idle ' + str(currentTime) + " " + str(cpuStats[3]) + "host ="+ thisClient+ "/n"
s.sendall(command)
params = urllib.urlencode({'cpuStats': cpuStats, 'thisClient': 1234})
headers = httplib.HTTPConnection(serverHost, port)
conn.request("POST", "", params, headers)
response = conn.response()
print response.status, response.reason
except IndexError:
break
i = i+1
s.close()
Instead of:
def loop_thru_cpus():
for i in cpuStats:
cpuStats = cpuStats[i]
cpuStats = json.dumps(cpuStats._asdict())
...
i = i+1
Try:
def loop_thru_cpus():
for stat in cpuStats:
stat = json.dumps(stat._asdict())
When you say
for i in cpuStats:
i takes on values from cpuStats. i is not an integer in this case. So i = i+1 makes no sense.
cpuStats = cpuStats[i]
This probably raised an IndexError (since i is not an integer), but for some reason you are not seeing the exception that was raised.
Also note that you are redefining cpuStats here, which is probably not what you want to do.
You probably do have indentation errors in your code. Running the code you posted through cat -A shows tabs (indicated below by ^I):
try:$
^I $
command = 'put cpu.usr ' + str(currentTime) + " " + str(cpuStats[0]) + "host ="+ thisClient+ "/n"$
...
params = urllib.urlencode({'cpuStats': cpuStats, 'thisClient': 1234})$
...
^I print response.status, response.reason$
$
^I except IndexError:$
break$
You can not mix tabs and spaces an indentation in Python code. Either use one or the other. The PEP8 style guide (and most code you see on the net) uses 4 spaces. Mixing tabs and spaces usually results in an IndentationError, but sometimes you don't get an error and just code that behaves in unexpected ways. So (if using the 4-spaces convention) be careful to use an editor that adds 4 spaces when the tab key is pressed.
Since you are not seeing the IndexError, you may not be seeing the IndentationError that should have been occurred either. How exactly are you running the problem?
I have the following script (below). which will return the status code of URLs. It loops through a file and tries to connect to each host. Only problem is that it obviously stops looping when it reaches an exception.
I have tried numerous things to put the how of it in a loop, but to no avail. Any thoughts?
import urllib
import sys
import time
hostsFile = "webHosts.txt"
try:
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
epoch = time.time()
epoch = str(epoch)
print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
sys.exit()
else:
f.close()
EDIT:
I've come up with this in the mean-time, any issues with this? (i'm still learning :p )...
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
try:
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
print epoch + "connection unsuccessful"
Thanks,
MHibbin
You could handle the exception where it is raised. Also, use a context manager when opening files, it makes for simpler code.
with open(hostsFile, 'r') as f:
for line in f:
line = line.strip()
if not line:
continue
epoch = str(time.time())
try:
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
You need to handle exception raised by urllib.urlopen(line), something like this.
try:
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
try:
conn = urllib.urlopen(line)
except IOError:
print "Exception occured"
pass
except IOError:
epoch = time.time()
epoch = str(epoch)
print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
sys.exit()
else:
f.close()
You could try catching the exception inside the while loop as something like this.
try:
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
try:
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except:
epoch = time.time()
epoch = str(epoch)
print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
except IOError:
pass
else:
f.close()
if data.find('!google') != -1:
nick = data.split('!')[ 0 ].replace(':','')
try:
gs = GoogleSearch(args)
gs.results_per_page = 1
results = gs.get_results()
for res in results:
sck.send('PRIVMSG ' + chan + " " + res.title.encode("utf8") + '\r\n')
sck.send('PRIVMSG ' + chan + " " + res.url.encode("utf8") + '\r\n')
print
except SearchError, e:
sck.send('PRIVMSG ' + chan + " " + "Search failed: %s" % e + " " + '\r\n')
Ok I'm trying to make the script wait a few seconds before another user can "!google" to prevent users from flooding the channel or the bot, not sure if I should use the sleep() function because that might stop the whole script, I just want to make it wait a few seconds before anyone can use "!google" again.
There is a sleep function inside the time module.
However, to make your script not block, you can call the time function in the time module and store that. If the current time is less than that plus, say, five seconds, don't allow them to use it.
For example:
last_google = 0
# somewhere later in the script where last_google is still in scope...
if data.find('!google') != -1:
if last_google + 5 < time.time():
# throttled
return
last_google = time.time()
# do something here