How to fix KeyError in python --> KeyError: 'message'? - python

This error usually should not appear, but recently when I run this, this error appears, and I have no clue how I will fix it.
Please if Anyone can help me to fix this error on this code below:
import requests
import json
from time import sleep
global OFFSET
OFFSET = 0
botToken = ""
global requestURL
global sendURL
requestURL = "http://api.telegram.org/bot" + botToken + "/getUpdates"
sendURL = "http://api.telegram.org/bot" + botToken + "/sendMessage"
def update (url):
global OFFSET
try:
update_raw = requests.get(url + "?offset=" + str(OFFSET))
update = update_raw.json()
result = extract_result(update)
if result != False:
OFFSET = result['update_id'] + 1
return result
else:
return False
except requests.exceptions.ConnectionError:
pass
def extract_result (dict):
result_array = dict['result']
if result_array == []:
return False
else:
result_dic = result_array[0]
return result_dic
def is_callback (dict):
if 'callback_query' in dict:
return True
def send_message (chatId, message):
requests.post(sendURL + "?chat_id=" + str(chatId) + "&text=" + message)
def send_message_button (chatId, message, buttonJSON):
requests.post(sendURL + "?chat_id=" + str(chatId) + "&reply_markup=" + buttonJSON + "&text=" + message)
#print (sendURL + "?chat_id=" + str(chatId) + "&reply_markup=" + buttonJSON + "&text=" + message)
while True:
newmessage = update (requestURL)
if newmessage != False:
if is_callback(newmessage) == True:
userchatid = newmessage['callback_query']['message']['chat']['id']
usertext = newmessage['callback_query']['message']['text']
username = newmessage['callback_query']['message']['chat']['first_name']
callback_data = newmessage['callback_query']['data']
send_message (userchatid, "Callback from " + callback_data + ", pressed by " + username)
else:
userchatid = newmessage['message']['chat']['id']
usertext = newmessage['message']['text']
username = newmessage['message']['chat']['first_name']
if usertext.lower() == "button":
buttonDict1 = {"text":"Knopf\n" + "hitest", "callback_data":"Knopf"}
buttonDict2 = {"text":"Knopf2", "callback_data":"Knopf2"}
buttonArr = {"inline_keyboard":[[buttonDict1, buttonDict2]]}
send_message_button (userchatid, "Hi " + username, json.dumps(buttonArr))
else:
send_message(userchatid, "You said: " + usertext)
sleep (1)
This is the error that appears to me after I run this bot
Line: 67
userchatid = newmessage['message']['chat']['id']
KeyError: 'message'

You catch the requests.exceptions.ConnectionError but don't handle it ( in the update function ), so now update does not return False as it returns nothing at all and can pass your check and cause havock.
Try to deal with the exception, or at least put a print in there to see if it's the one causing you issues, good luck!

Related

Python Won't Enter Try/Except In For Loop On 50th Iteration

First off I want to apologize to everyone who's about to read this code... I know it's a mess.
For anyone who is able to decipher it: I have a list of ~16,500 website URL's that I am scraping and then using googles NLP to categorize. The list of URL's is created with the following chunk of code, as far as I can tell nothing is broken here.
url_list = open("/Users/my_name/Documents/Website Categorization and Scrapper Python/url_list copy", "r")
indexed_url_list = url_list.readlines()
clean_url_list = []
clean_url_list = [x[:-1] for x in indexed_url_list]
When I print the length of this list it correctly gives me the count of ~16,500.
The main block of code is as follows:
for x in clean_url_list:
print('1')
url = x
print('1.1')
try:
r = scraper.get(url, headers = headers,)
print('1.2')
soup = BeautifulSoup(r.text, 'html.parser')
print('1.3')
title = soup.find('title').text
print('1.4')
description = soup.find('meta', attrs={'name': 'description'})["content"]
print('2')
if "content" in str(description):
description = description.get("content")
else:
description = ""
h1 = soup.find_all('h1')
h1_all = ""
for x in range (len(h1)):
if x == len(h1) -1:
h1_all = h1_all + h1[x].text
else:
h1_all = h1_all + h1[x].text + ". "
paragraphs_all = ""
paragraphs = soup.find_all('p')
for x in range (len(paragraphs)):
if x == len(paragraphs) -1:
paragraphs_all = paragraphs_all + paragraphs[x].text
else:
paragraphs_all = paragraphs_all + paragraphs[x].text + ". "
h2 = soup.find_all('h2')
h2_all = ""
for x in range (len(h2)):
if x == len(h2) -1:
h2_all = h2_all + h2[x].text
else:
h2_all = h2_all + h2[x].text + ". "
h3 = soup.find_all('h3')
h3_all = ""
for x in range (len(h3)):
if x == len(h3) -1:
h3_all = h3_all + h3[x].text
else:
h3_all = h3_all + h3[x].text + ". "
allthecontent = ""
allthecontent = str(title) + " " + str(description) + " " + str(h1_all) + " " + str(h2_all) + " " + str(h3_all) + " " + str(paragraphs_all)
allthecontent = str(allthecontent)[0:999]
print(allthecontent)
except Exception as e:
print(e)
When I run this it successfully categorizes the first 49 URL's, but ALWAYS stops on the 50th, no matter what URL it is. No error is thrown, and even if it did the try/except should handle it. Using the print statements to debug it seems to not enter the "try" section on the 50th iteration for whatever reason and it's always the 50th iteration
Any help would be much appreciated and I hope you have some good eye wash to wipe away the code you just had to endure.
I helped look at this at work. The actual issue was a bad 50th url that would not return. Adding a timeout allowed the code to escape the try/catch block and move on to the next url in a manageable fashion.
try:
r = scraper.get(url, headers = headers, timeout=5)
except:
continue # handle next url in list

Multicast Network Trigger In Python Script

I'm trying to add a multicast network trigger for a camera set up. Currently I am using a conf. file called multicast-trigger.conf
[multicast-trigger]
address = 224.1.1.1
port = 600
payload = 0x05AA9544
and a python script called app_ext_multicast_trigger.py which takes the information from the conf file to send a multicast network trigger.
import sys, os, logging, json, ConfigParser, socket
sys.path.append('/home/root/ss-web')
from camconstants import *
class AppExt(object):
pkg_name = "Multicast network trigger "
pkg_version = "v1.0"
mt_config = None
# =============================================================================
# Package and URL registration logic
# =============================================================================
def __init__(self, _app, _cam, _ci, register_url_callback):
"""
Register new URLs with camera's webserver.
"""
urls = [
( '/trigger2', self.trigger2, "Sends a multicast network trigger packet to trigger all the cameras on the same local network, including trigging this camera." ),
( '/get_multicast_configuration', self.get_multicast_configuration, "Returns a JSON encoded dictionary of the camera's /etc/multicast-trigger.conf file contents." ),
]
register_url_callback(self.pkg_name, self.pkg_version, urls)
self.mt_config = self._mt_read_multicast_config_file('/etc/multicast-trigger.conf')
logging.debug("Multicast trigger URL added: %s" % repr(self.mt_config))
# =============================================================================
# Multicast trigger helper methods
# =============================================================================
def _mt_read_multicast_config_file(self, fn):
"""
Returns dictionary with multicast trigger configuration information read from file fn.
"""
if not os.path.exists(fn):
logging.error("Missing file: %s" % fn)
return None
config = {}
try:
config_parser = ConfigParser.SafeConfigParser()
config_parser.read(fn)
c = config_parser.items('multicast-trigger')
for (key, value) in c:
if key == 'address':
config[key] = value
elif key in ('port', 'payload'):
config[key] = int(value, 0)
except Exception, e:
logging.error("Bad file format: %s" % fn)
logging.error("Ignoring multicast-trigger parameters due to exception - %s" % str(e))
return None
return config
# =============================================================================
# Exposed URLs - URL matches method name
# =============================================================================
def trigger2(self):
"""
Sends a multicast network packet to trigger all cameras on the same local network.
"""
if self.mt_config == None:
logging.error("Missing file: %s" % fn)
ret = CAMAPI_STATUS_INVALID_PARAMETER
else:
try:
logging.debug("Triggering cameras by sending multicast packet: address %s port %d with payload 0x%x" % (self.mt_config['address'], self.mt_config['port'], self.mt_config['payload']))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
data=""
v = self.mt_config['payload']
for i in range(4):
data = chr(v & 0xFF) + data
v = v >> 8
sock.sendto(data, (self.mt_config['address'], self.mt_config['port']))
ret = CAMAPI_STATUS_OKAY
except Exception, e:
logging.error("Multi-camera trigger error due to exception - %s" % str(e))
ret = CAMAPI_STATUS_INVALID_PARAMETER
return json.dumps(ret)
def get_multicast_configuration(self):
"""
Returns a JSON encoded dictionary of the camera's /etc/multicast-trigger.conf file.
"""
logging.debug("Returning multicast trigger configuration: %s" % repr(self.mt_config))
return json.dumps(self.mt_config)
This works well and when I go to the ip address of the camera /trigger2 it will send a packet to trigger the other camera on the same network. Now I also have a different piece of equipment which will also trigger the camera via a python script called app_ext_b1 and when run from the device will trigger the camera and save to the device.
import sys, os, logging, json, time, threading, flask, glob, datetime, subprocess, math
from urllib import unquote
from camconstants import *
import tarfile
import sys, os, time, json, datetime, math
class CameraException(Exception):
def __init__(self, code, message):
self.Code = code
self.Message = message
super(CameraException, self).__init__(message)
def AsObject(self):
return { "Code" : self.Code, "Message" : self.Message }
class ExecutionContext:
def __init__(self, id, path):
self.Id = id
self.Error = None
self.path = path
if not self.path.endswith("/"):
self.path = "/" + self.path
self.Key = None
self.Logs = []
self.Results = {}
self._from = time.time()
self.add_log("Id: " + self.Id)
def get_path(self, fname):
return self.path + fname
def execute(self, key, method):
self.Key = key
self.add_log("Executing " + key)
try:
method(self)
except CameraException as camException:
self.add_log("Failed task " + key)
self._add_error(key, camException)
return False
except Exception as error:
self.add_log("Failed task " + key)
self._add_error(key, CameraException(-1, str(error)))
return False
self.add_log("Finished " + key)
return True
def _add_error(self, key, error):
self.Error = error
self.add_log("Error: " + str(error) )
def add_local(self, key, result):
if not self.Key in self.Results:
self.Results[self.Key] = {}
local_result = self.Results[self.Key]
local_result[key] = result
def add_log(self, message):
delta = str(int(1000*(time.time() - self._from))).rjust(6)
fullmsg = str(delta) + ": " + str(self.Key) + " " + message
self.Logs.append(fullmsg)
def add_state(self, save_state, camStatus):
state = {
"save_state": save_state,
"time": int(time.time() * 1000),
"camerastatus": camStatus,
}
stateStr = json.dumps(state)
self.add_log("Setting state to: " + stateStr)
progress_file = open(self.get_path(self.Id + ".state"), "w")
progress_file.write(stateStr)
progress_file.close()
def get_state(self):
try:
stateJson = open(self.get_path(self.Id + ".state"), "r").read()
self.add_log("State: " + stateJson)
state = json.loads(stateJson)
return state
except:
return None
def save_logs(self):
try:
self.Logs.append("Results:\n" + json.dumps(self.Results))
progress_file = open(self.get_path(self.Id + ".log"), "w")
progress_file.write("\n".join(self.Logs))
progress_file.close()
except:
pass
class TriggerInfo:
def __init__(self, triggerTime, releaseTime, fps):
self.TriggerTime = triggerTime
if releaseTime <=0:
releaseTime = triggerTime + releaseTime
self.ReleaseTime = releaseTime
self.Fps = fps
def getFrameForDeltaReleaseMs(self, deltaReleaseMs):
return self.convertMsToFrames(((self.ReleaseTime + float(deltaReleaseMs) / 1000.0) - self.TriggerTime)* 1000)
def convertMsToFrames(self, ms):
return int(self.Fps * float(ms)/ 1000.0)
class FrameSaver:
def __init__(self, cam, ec, params):
self.Cam = cam
self.Tar = None
self.Ec = ec
self.LastImageModified = None
self.id = params["id"]
frameTo = params["frameTo"]
curFrame = params["frameFrom"]
self.Frames = [curFrame]
interval = params["frameInterval"]
curFrame += interval
while curFrame < frameTo:
self.Frames.append(curFrame)
curFrame += interval
self.Frames.append(frameTo)
if not frameTo in self.Frames:
self.Frames.append(frameTo)
def save_frames(self):
for frameNumber in self.Frames:
self._remove_image()
self.Ec.add_log("reviewing frame")
self.Cam.review_frame(1, frameNumber)
self._save_image(frameNumber)
self.Tar.close()
def _remove_image(self):
os.system("rm /home/root/ss-web/static/images/image.jpg")
def _save_image(self, frameNumber):
self.Ec.add_log("save image to tar")
# http://10.11.12.13/static/images/image.jpg
path = "/home/root/ss-web/static/images/image.jpg"
tar = self.create_archive_if_not_exists()
start_time = time.time()
while not os.path.exists(path):
time.sleep(0.050)
if (time.time() - start_time) > 1:
raise CameraException(-1, "Fullball flight: failed to save image for framenumber " + str(frameNumber))
tar.add(path, str(frameNumber) + ".jpg")
def create_archive_if_not_exists(self):
if self.Tar == None:
self.Tar = tarfile.open("/mnt/sdcard/DCIM/" + self.id + ".Frames.tar", "w")
return self.Tar
class CameraCaptureFlow:
def __init__(self, id, camera, options):
self.Camera = camera
self.Options = options
self._state_key = "Unknown"
self.Id = id
self.Fps = 1
self.TriggerInfo = None
self.ReleaseFrame = None
self.SaveFps = None
self.StartFrame = None
self.EndFrame = None
self.PretriggerFillLevel = 100
self.ExecutionContext = ExecutionContext(id, "/mnt/sdcard/DCIM/")
self.ExecutionContext.add_log("Options:\n" + json.dumps(options))
def run(self):
# A trigger flow has already been initiated this id - return - note, it can be None if we are generating the file on top of the old one
if self.ExecutionContext.get_state() != None:
return
self._add_state("Started")
if not self._execute("Info", self.info):
return
if not self._execute("trigger", self.trigger):
self._execute("reinit", self.reinit)
return
if not self._execute("saveselected", self.save_selected):
self._execute("reinit", self.reinit)
return
if not self._execute("remaining ball flight", self.save_remaning_ballflight):
self._execute("reinit", self.reinit)
return
if not self._execute("reinit", self.reinit):
return
self._add_state("Completed")
return
def info(self, context):
self.CurrentConfig = self.Camera.get_current_settings()
self.PretriggerFillLevel = self.Camera.get_pretrigger_fill_level()
self.CamInfo = self.Camera.get_storage_info()
if not "available_space" in self.CamInfo:
raise CameraException(3, "StorageUnavaible")
if self.CamInfo["available_space"] < 50000000:
raise CameraException(4, "StorageInsufficient")
self.Fps = self.CurrentConfig["frame_rate"]
def TriggerCamera(self, context):
if hasattr(self.Camera, "trigger_hw"):
context.add_log("using hardware trigger")
return self.Camera.trigger_hw(None)
return self.Camera.trigger(None)
def wait_for_buffer_ready(self, context, releaseOffset, postCaptureMs):
delta_ms_release = int(1000*(time.time() - releaseOffset))
waitTime = postCaptureMs - delta_ms_release
context.add_log("Time since release : " + str(delta_ms_release) + "ms" )
context.add_log("Post capture time : " + str(postCaptureMs) + "ms" )
if waitTime > 0:
context.add_log("Time since release is less than required for post capture duration")
context.add_log("waiting " +str(waitTime + 100) + "ms to fill up buffers")
time.sleep((waitTime)/1000.0 + 0.1)
def trigger(self, context):
releaseOffset = self.Options["releaseOffsetTime"]
self.wait_for_buffer_ready(context, releaseOffset, self.Options["postCaptureMs"])
context.add_log("Triggering camera")
before = time.time()
triggerResult = self.TriggerCamera(context)
after = time.time()
triggerTime = (before + after) / 2
self.TriggerInfo = TriggerInfo(triggerTime, releaseOffset, self.Fps)
context.add_local("triggerTime", triggerTime)
context.add_local("relaseTime", self.TriggerInfo.ReleaseTime)
if (triggerResult != CAMAPI_STATUS_OKAY):
self._stop_if_saving()
raise CameraException(6, "TriggerFail")
context.add_log("Waiting for camera to trigger")
self.wait_for_status(lambda status: status == CAMAPI_STATE_TRIGGERED, 1)
context.add_log("Waiting for camera to finish triggering")
self.wait_for_status(lambda status: status != CAMAPI_STATE_TRIGGERED, 2)
context.add_log("Triggering finished")
def _stop_if_saving(self):
camStatus = self.Camera.get_camstatus()["state"]
if camStatus == CAMAPI_STATE_SELECTIVE_SAVING:
self.Camera.save_stop(discard_unsaved=True)
self.wait_for_status(lambda status: status == CAMAPI_STATE_REVIEWING or status == CAMAPI_STATE_RUNNING, 5)
raise CameraException(5, "TriggerFailCameraIsSaving")
def save_selected(self, context):
preCapMs = self.Options["preCaptureMs"]
postCapMs = self.Options["postCaptureMs"]
self.ReleaseFrame = self.TriggerInfo.getFrameForDeltaReleaseMs(0)
self.StartFrame = self.TriggerInfo.getFrameForDeltaReleaseMs(-preCapMs)
self.EndFrame = self.TriggerInfo.getFrameForDeltaReleaseMs(postCapMs)
context.add_log("ReleaseFrame: " + str(self.ReleaseFrame))
context.add_local("release_frame", self.ReleaseFrame)
context.add_local("start_frame", self.StartFrame)
context.add_local("end_frame", self.EndFrame)
self._validateSaveParams(context, self.StartFrame, self.EndFrame)
save_params = {}
save_params['buffer_number'] = 1
save_params['start_frame'] = self.StartFrame
save_params['end_frame'] = self.EndFrame
save_params['filename'] = str(self.Options["id"])
context.add_log("selective_save_params\n" + json.dumps(save_params))
before = time.time()
if self.Camera.selective_save(save_params) != CAMAPI_STATUS_OKAY:
raise CameraException(9, "SelectiveSaveFailed")
context.add_log("Waiting for camera to start saving")
self.wait_for_status(lambda status: status == CAMAPI_STATE_SELECTIVE_SAVING, 1)
context.add_log("Waiting for camera to finish saving")
self.wait_for_status(lambda status: status != CAMAPI_STATE_SELECTIVE_SAVING, 300)
context.add_log("Camera finished saving")
after = time.time()
self.SaveFps = math.ceil((self.EndFrame - self.StartFrame) / (after - before))
context.add_local("save_fps", self.SaveFps)
context.add_log("save fps: " + str(self.SaveFps))
def save_remaning_ballflight(self, context):
context.add_log("Checking options")
frameIntervalMs = self.Options["singleFrameCaptureIntervalMs"]
frameCaptureEndMs = self.Options["singleFrameCaptureEndMs"]
# we wish to combine the existing video with the next frames
if frameIntervalMs == None:
return
frameCaptureStartMs = self.Options["postCaptureMs"] + frameIntervalMs
frameInterval = self.TriggerInfo.convertMsToFrames(frameIntervalMs)
frameFrom = self.TriggerInfo.getFrameForDeltaReleaseMs(frameCaptureStartMs)
frameEnd = self.TriggerInfo.getFrameForDeltaReleaseMs(frameCaptureEndMs)
frameSaverParams = {
"id" : self.Id,
"frameFrom" : frameFrom,
"frameTo" : frameEnd,
"frameInterval" : frameInterval
}
frameSaver = FrameSaver(self.Camera, self.ExecutionContext, frameSaverParams)
frameSaver.save_frames()
def _validateSaveParams(self, context, startFrame, endFrame):
duration = self.CurrentConfig["duration"]
pretrigger = self.CurrentConfig["pretrigger"]
context.add_log("Pretrigger Level " + str(self.PretriggerFillLevel))
preTriggerBufferSeconds = duration * pretrigger / 100.0 * (float(self.PretriggerFillLevel) *1.0001 / 100.0)
context.add_log("preTriggerBufferSeconds " + str(preTriggerBufferSeconds))
postTriggerBuffserSeconds = duration * (1 - pretrigger / 100)
minFrame = -preTriggerBufferSeconds * self.Fps + 10
maxFrame = postTriggerBuffserSeconds * self.Fps - 10
if startFrame < minFrame:
msg = "Startframe: " + str(startFrame) + " is less than minimum frame" + str(minFrame)
context.add_log(msg)
raise CameraException(7, "OutOfBufferStartFrame")
if endFrame > maxFrame:
msg = "Endframe: " + str(endFrame) + " is larger than maximum frame" + str(maxFrame)
context.add_log(msg)
raise CameraException(8, "OutOfBufferEndFrame")
def reinit(self, context):
self.Camera.run(self.CurrentConfig)
def wait_for_status(self, predicate, timeout):
before = time.time()
lastStateUpdate = time.time()
lastState = None
while True:
status = self.Camera.get_camstatus()["state"]
deltaTime = time.time() - lastStateUpdate
if status != lastState or deltaTime > 1:
lastState = status
self._update_state(status)
lastStateUpdate = time.time()
if predicate(status):
return True
if time.time() - before > timeout:
return False
time.sleep(.333)
return False
def _update_state(self, camStatus):
self.ExecutionContext.add_state(self._state_key, camStatus)
def _add_state(self, save_state_key):
self._state_key = save_state_key
try:
camStatus = self.Camera.get_camstatus()["state"]
except:
camStatus = CAMAPI_STATE_UNCONFIGURED
self._update_state(camStatus)
def _execute(self, key, method):
success = self.ExecutionContext.execute(key, method)
if not success:
self._add_state("Failed")
return success
def save_logs(self):
self.ExecutionContext.save_logs()
class AppExt(object):
pkg_name = "B1 application extension"
pkg_version = "v1.8.1"
app = None
cam = None
ci = None
def __init__(self, _app, _cam, _ci, register_url_callback):
self.app = _app
self.cam = _cam
self.ci = _ci
urls = [
(
'/get_extension_b1_version',
self.get_version,
"returns version of B1 extension"
),
(
'/get_time_offset',
self.get_time_offset,
"Gets the camera time offset"
),
(
'/trigger_and_save',
self.trigger_and_save,
"Triggers the camera, and save the selected capture part"
),
(
'/get_trigger_and_save_progress',
self.get_trigger_and_save_progress,
"Returns the progress of the trigger and save process"
),
(
'/ensure_camera_ready',
self.ensure_camera_ready,
"Will check that the camera can save and has enough available space"
)
]
register_url_callback(self.pkg_name, self.pkg_version, urls)
def get_version(self):
return json.dumps(
{
"extensionVersion": AppExt.pkg_version
})
def get_time_offset(self):
timeOffset = time.time()
return json.dumps(
{
"timeOffset": timeOffset
})
def trigger_and_save(self):
request = {}
try:
request["id"] = str(self.get_arg('id'))
request["releaseOffsetTime"] = float(self.get_arg('releaseOffsetTime'))
request["preCaptureMs"] = self.get_int('preCaptureMs')
request["postCaptureMs"] = self.get_int('postCaptureMs')
request["singleFrameCaptureIntervalMs"] = self.get_int('singleFrameCaptureIntervalMs', True)
request["singleFrameCaptureEndMs"] = self.get_int('singleFrameCaptureEndMs', True)
captureFlow = CameraCaptureFlow(request["id"], self.cam, request)
timeStart = time.time()
captureFlow.run()
totalTime = int((time.time() - timeStart) * 1000)
ec = captureFlow.ExecutionContext
state = ec.get_state()
result = {
"id" : ec.Id,
"results" : ec.Results,
"logs" : ec.Logs,
"state" : state,
"fps" : captureFlow.Fps,
"release_frame" : captureFlow.ReleaseFrame,
"start_frame" : captureFlow.StartFrame,
"end_frame" : captureFlow.EndFrame,
"save_fps": captureFlow.SaveFps,
"trigger_offset" : captureFlow.TriggerInfo.TriggerTime,
"total_time" : totalTime
}
if ec.Error != None:
result["capture_error"] = ec.Error.AsObject()
captureFlow.save_logs()
return json.dumps(result)
except Exception as E:
request["error"] = str(E)
captureFlow.save_logs()
return json.dumps(request)
def get_arg(self, arg, isOptional = False):
req = flask.request.args
try:
val = req.get(arg)
return str(unquote(val))
except:
if isOptional:
return None
raise Exception("Could not find " + arg + " in the request")
def get_int(self, arg, isOptional = False):
try:
val = self.get_arg(arg)
return int(val)
except Exception as E:
if isOptional:
return None
raise E
def get_trigger_and_save_progress(self):
request = {}
try:
trigger_id = str(self.get_arg('id'))
exec_context = ExecutionContext(trigger_id, "/mnt/sdcard/DCIM/")
result = {}
result["id"] = trigger_id
state = None
timeBefore = time.time()
while state == None and time.time() - timeBefore < 1:
state = exec_context.get_state()
if state == None:
time.sleep(0.017)
result["state"] = state
return json.dumps(result)
except Exception as E:
request["error"] = str(E)
return json.dumps(request)
def ensure_camera_ready(self):
try:
camInfo = self.cam.get_storage_info()
if "available_space" not in camInfo:
raise CameraException(3, "StorageUnavailable")
if camInfo["available_space"] < 50000000:
raise CameraException(4, "StorageInsufficient")
self._validate_storage_writable()
except CameraException as camEx:
return json.dumps({ "Success": False, "Code": camEx.Code, "Message": camEx.Message })
except Exception as ex:
return json.dumps({ "Success": False, "Code": -1, "Message": str(ex) })
return json.dumps({ "Success": True, "Code": 0, "Message": None})
def _validate_storage_writable(self):
try:
timestamp = str(time.time())
fname = "/mnt/sdcard/DCIM/timestamp.txt"
storageFile = open(fname, "w")
storageFile.write(timestamp)
storageFile.close()
storageContent = open(fname, "r").readlines()[0]
if (storageContent != timestamp):
raise "could not write file"
except:
raise CameraException(2, "StorageReadonly")
The device is a radar unit and only allows for one camera to be synced at a given time. It reads the object it's tracking, tells the camera when to trigger and will save the file to ipad I am running the radar on. I would like to have a second camera trigger which would save the video on the second cameras interal memory. Doing this I can upload and merge with the other video file later. This would save me from manually having to trigger the second camera each time and trim the video lengths to match up. This would also allow me to connect the cameras wirelessly using travel routers. I was wondering how I could add the app_ext_multicast_trigger and multicast-trigger.conf to the app_ext_b1 python script so when the radar unit runs the app_ext_b1 python script on the camera it will also send out a multicast network packet to trigger camera two?

displaying output in html with a python code

I'm still learning the ins and outs of python and my goal right now is I want to display the outputs of my python code into a html webpage. How can I do that, provided that I already have a html webpage, but instead of the output displaying on the webpage itself, it displays on the console only.
app = Flask(__name__)
__author__ = 'kai'
from flask import Flask, render_template, request
import urllib.parse
import requests
#app.route('/')
def index():
return render_template('index.html')
#app.route('/hello', methods=['POST'])
def hello():
while True:
orig = request.form['starting']
if orig == "quit" or orig == "q":
break
dest = request.form['destination']
if dest == "quit" or dest == "q":
break
url = main_api + urllib.parse.urlencode({"key": key, "from": orig, "to":dest})
print("URL: " +(url))
json_data = requests.get(url).json()
json_status = json_data["info"]["statuscode"]
if json_status == 0:
print("API Status: " + str(json_status) + " = A successful route call.\n")
print("=============================================")
print("Directions from " + (orig) + " to " + (dest))
print("Trip Duration: " + (json_data["route"]["formattedTime"]))
print("-----------------Distance in different units-----------------")
print("Kilometers: " + str("{:.2f}".format((json_data["route"]["distance"])*1.61)))
print("Miles: " + str(json_data["route"]["distance"]))
print("Yards: " + str("{:.2f}".format((json_data["route"]["distance"])*1760)))
print("-----------------Fuel Used in different units-----------------")
print("Fuel Used (Ltr): " + str("{:.2f}".format((json_data["route"]["fuelUsed"])*3.78)))
print("Fuel Used (US Gal): " + str(json_data["route"]["fuelUsed"]))
print("Fuel Used (Imperial Gal): " + str("{:.2f}".format((json_data["route"]["fuelUsed"])*0.8327)))
print("=============================================")
for each in json_data["route"]["legs"][0]["maneuvers"]:
print((each["narrative"]) + " (" + str("{:.2f}".format((each["distance"])*1.61) + " km)"))
print("=============================================\n")
elif json_status == 402:
print("**********************************************")
print("Status Code: " + str(json_status) + "; Invalid user inputs for one or both locations.")
print("**********************************************\n")
elif json_status == 611:
print("**********************************************")
print("Status Code: " + str(json_status) + "; Missing an entry for one or both locations.")
print("**********************************************\n")
else:
print("************************************************************************")
print("Try Again")
print("************************************************************************\n")
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 3000)

Python 2.7.12 netaddr.IPNetwork AddrFormatError

Version: Python 2.7.12
OS: Ubuntu 16.04.1
Kernel: 4.4.0-31-generic #50-Ubuntu SMP
Arch: ppc64le
netaddr.IPNetwork fails with:
netaddr.core.AddrFormatError: invalid IPNetwork '9.2.0.0/16'
I have a function:
def innet2(ip, net):
print("ip: "+ip, "net: " + net)
ip = IPAddress(ip).value
print("ipnew: " + str(ip))
network = IPNetwork(net)
print("network-first: " + str(network.first))
print("network-last: " + str(network.last))
if ip >= network.first and ip <= network.last:
return True
else:
return False
If I call this function at the beginning of my program for debug purposes
and it executes properly:
if __name__ == "__main__":
FLAGS(sys.argv)
startSSH()
print ("service ssh finished")
isParamReady = False
hostsStr = ""
isChef = False
for i in range(0, 100):
time.sleep(20)
print("test: " + str(innet("9.2.132.186", "9.2.0.0/16")))
print("test2: " + str(innet2("9.2.132.186", "9.2.0.0/16")))
print("test2: " + str(innet2("10.1.3.2", "9.2.0.0/16")))
isParamReady, hostsStr, isChef = **getHostIpStr()**
break
if (isParamReady is True and isChef is True):
execCommand(hostsStr)
else:
waitOrExit()
When it is called from getHostIPStr() it generates the AddrFormatError
def getHostIpStr():
hostsStr = "-host "
isChef = False
print("namespace= " + namespace)
print("FLAGS.job_name= " + FLAGS.job_name)
print("FLAGS.network= " + FLAGS.network)
ps_pods = v1.list_namespaced_pod(namespace, label_selector="job="
+ FLAGS.job_name)
job = v1batch.list_namespaced_job(namespace, label_selector="job="
+ FLAGS.job_name)
worker_num = job.items[0].spec.parallelism
items = ps_pods.items
print("items= ", len(items))
print("worker_num= " + str(worker_num))
if (len(items) < worker_num):
return False, "", False
for i in range(0, len(items)):
podIp = items[i].status.pod_ip
print("podIp:" + podIp)
print("localIp:" + localIp)
if (i == 0 and podIp == localIp):
isChef = True
hostIPs = getIp(podIp)
net = FLAGS.network
print("len of Ips: " + str(len(hostIPs)))
for j in range(0, len(hostIPs)):
print("j: " + str(j), "hostIPs[j]: " + hostIPs[j],
"network: " + FLAGS.network)
ip = hostIPs[j];
res = innet2(ip, net)
if (res is True):
podIp = hostIPs[j]
hostsStr = hostsStr + podIp
break
if (i < len(items)-1):
hostsStr = hostsStr + ","
return True, hostsStr, isChef
I discovered my problem.
I was passing a quoted network address and IPNetwork was failing when trying to convert the octet from of ip address into decimal format because the first octet had the string quote in it.

Python - WindowName for Compare with List

I am currently blocked on a point of a program in Python.
I wish to compare in a list, the WindowName event to launch directives.
Example:
import win32api
import pyHook
liste = ["Google", "Task"]
if event.WindowName == liste:
Screenshot ()
return True
else:
return False
Complete code, he work:
def OnMouseEvent(event):
global interval
data = '\n[' + str(time.ctime().split(' ')[3]) + ']' \
+ ' WindowName : ' + str(event.WindowName)
data += '\n\tButton:' + str(event.MessageName)
data += '\n\tClicked in (Position):' + str(event.Position)
data += '\n===================='
global t, start_time, pics_names
"""
Code Edit
"""
t = t + data
if len(t) > 300:
ScreenShot()
"""
Finish
"""
if len(t) > 500:
f = open('Logfile.txt', 'a')
f.write(t)
f.close()
t = ''
if int(time.time() - start_time) == int(interval):
Mail_it(t, pics_names)
start_time = time.time()
t = ''
return True
else:
return False
When i edit the code in """ doesn't work :
t = t + data
liste = ["Google", "Task"]
if event.WindowName == liste:
ScreenShot()
He return :
File "C:\Python26\lib\site-packages\pyHook\HookManager.py", line 324, in MouseSwitch func = self.mouse_funcs.get(msg) TypeError: an integer is required
I test this :
HookManager: func = self.keyboard_funcs.get(msg) to: func=self.keyboard_funcs.get( int(str(msg)) )
But is don't work, i think i note all problem.
Thanks for you help in advance :)

Categories

Resources