Perfectly running python script gives error when run from web.py - python

I have the following python script which runs perfectly if run separately:
import arcpy
val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
print str(val)
I want to expose this as a web service and so I installed web.py and wrote the following code for code.py. but it gives errors(when invoked. compiles fine).
import web
import arcpy
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
return str(val)
if __name__ == "__main__":
app.run()
When i invoke this using http://localhost:8080, i get the following error:
<class 'arcgisscripting.ExecuteError'> at /
ERROR 000582: Error occurred during execution.
Python C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py in GetCellValue, line 8460
Web GET http://localhost:8080/
Traceback (innermost first)
C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py in GetCellValue
be returned."""
try:
retval = convertArcObjectToPythonObject(gp.GetCellValue_management(*gp_fixargs((in_raster, location_point, band_index), True)))
return retval
except Exception, e:
raise e ...
#gptooldoc('GetRasterProperties_management', None)
def GetRasterProperties(in_raster=None, property_type=None):
"""GetRasterProperties_management(in_raster, {property_type})
Returns the properties of a raster dataset.
I have tried Debugging it in many ways, the code .py runs fine if i just return a "hello". The library i am using is a ArcGIS Geoprocessing toolbox library.
Any ideas as to what might be wrong?

That looks like an error from the third party module. Try finding out what that error code (000582) actually means to the application.

Related

The action didn't produce the valid JSON object in python file

I am using two actions of IBM cloud function - write1 and write2 (both using PYTHON).
I created a sequence that should pass value from write1 to write2.
I wrote a PYTHON code in write1 action but is throws some JSON error.
Write 1 Python File:*
import os
import sys
import json
import requests
import ibm_boto3
from ibm_botocore.client import Config, ClientError
cos = ibm_boto3.resource("s3",
ibm_api_key_id='my-api-key',
ibm_service_instance_id='my-instance-id',
config=Config(signature_version="oauth"),
endpoint_url='https://s3.eu-gb.cloud-object-storage.appdomain.cloud'
)
def get_item(bucket_name, item_name):
a={"Retrieving item from bucket":bucket_name , "key": item_name}
print(json.dumps(a))
try:
file = cos.Object(bucket_name, item_name).get()
return file["Body"].read()
except ClientError as be:
w={"CLIENT ERROR":be}
print(json.dumps(w))
except Exception as e:
y={"Unable to retrieve file contents":e}
print(json.dumps(y))
def test():
x = get_item('cloud-college-bucket0','abc.txt')
print(x.decode('utf-8'))
if x is not None:
string_in_uppercase = x.upper();
n={"String in Uppercase =":string_in_uppercase.decode('utf-8')}
b=json.dumps(n)
print(b)
def main(dict):
return test()
if __name__ == '__main__':
main()
Error it throws:
Results:
{
"error": "**The action did not produce a valid JSON response**: null\n"
}
Logs:
[
"2019-10-08T13:01:56.339677Z stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the post() function from 'ibm_botocore.vendored.requests'. This is not a public API in ibm_botocore and will be removed in the future. Additionally, this version of requests is out of date. We recommend you install the requests package, 'import requests' directly, and use the requests.post() function instead.",
"2019-10-08T13:01:56.339748Z stderr: DeprecationWarning",
"2019-10-08T13:01:56.339755Z stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/models.py:169: DeprecationWarning:
Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working",
"2019-10-08T13:01:56.339759Z stderr: if isinstance(hook, collections.Callable):",
"2019-10-08T13:01:56.339678Z stdout: {\"Retrieving item from bucket\": \"cloud-college-bucket0\", \"key\": \"abc.txt\"}",
"2019-10-08T13:01:56.339772Z stdout: hello friends",
"2019-10-08T13:01:56.339776Z stdout: {\"String in Uppercase =\": \"HELLO FRIENDS\"}",
"2019-10-08T13:01:56.340Z stderr: The action did not initialize or run as expected. Log data might be missing."
]
It says import request which I did but still the problem persists.
I also say use request.post function but how and where to use is what I am unable to understand. And how to solve this JSON issue?
And the desired output is shown in the logs.
From the first outset, I could see that the test function only prints the JSON but never returns it. But you if you see the sample Python action, it always should return a JSON
import sys
def main(dict):
return { 'message': 'Hello world' }
Also, you can check the supported packages list with Python runtime here before using them in your action.
If you have a package that is not in the list, you can always package Python code with a virtual environment in .zip files or Packaging code in Docker images

module flask will not launch

project interpreter and local env IMAGEI'm having a real problem with using the module flask , I've tried a lot of the solutions here on the forum but none has worked.
I can see flask is installed
pip list - showing flask
in the setting the module flask is installed in project interpreter
when I type the code I can see the module comes up
However when I launch the code I get an error
No module named 'flask'
I've tried to re-install pycharm
I've tried to uninstall and install flask again
still the same problem. Any advice ?
The file name vsearch.py
Here is the code:
from flask import Flask, render_template, request, escape
app = Flask(__name__)
def search4words(phrase: str, letters: str) -> set:
return set(letters).intersection(set(phrase))
def log_request(req: 'flask_request', res: str) -> None:
with open('vsearch.log', 'a') as log:
print(req.form, req.remote_addr, req.user_agent, res, file=log,
sep='|')
#app.route('/search4', methods=['POST'])
def do_search() -> 'html':
phrase = request.form['phrase']
letters = request.form['letters']
title = 'Here are your results:'
results = str(search4words(phrase, letters))
log_request(request, results)
return render_template('results.html', the_phrase=phrase,
the_letters=letters, the_title=title,
the_results=results,)
#app.route('/')
#app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html', the_title='Welcome back
AGAIN!!!!!')
#app.route('/viewlog')
def view_the_log() -> 'html':
contents = []
with open('vsearch.log') as log:
for line in log:
contents.append([])
for item in line.split('|'):
contents[-1].append(escape(item))
titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results')
return render_template('viewlog.html',
the_title = 'View log',
the_row_titles = titles,
the_data = contents,)
if __name__ == '__main__' :
app.run(debug=True)
Your issue was attempting to run vsearch.py through terminal, rather than through PyCharm's interpreter (which was correctly installed). In order to utilize the virtual environment, you should configure it to be used correctly when running your code.
There are multiple ways of activating your virtual environment, so please find that which is applicable to your project. A good source for this would be https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/.

Executing program via pyvmomi creates a process, but nothing happens after that

I'm studying vCenter 6.5 and community samples help a lot, but in this particular situation I can't figure out, what's going on. The script:
from __future__ import with_statement
import atexit
from tools import cli
from pyVim import connect
from pyVmomi import vim, vmodl
def get_args():
*Boring args parsing works*
return args
def main():
args = get_args()
try:
service_instance = connect.SmartConnectNoSSL(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
vm = content.searchIndex.FindByUuid(None, args.vm_uuid, True)
creds = vim.vm.guest.NamePasswordAuthentication(
username=args.vm_user, password=args.vm_pwd
)
try:
pm = content.guestOperationsManager.processManager
ps = vim.vm.guest.ProcessManager.ProgramSpec(
programPath=args.path_to_program,
arguments=args.program_arguments
)
res = pm.StartProgramInGuest(vm, creds, ps)
if res > 0:
print "Program executed, PID is %d" % res
except IOError, e:
print e
except vmodl.MethodFault as error:
print "Caught vmodl fault : " + error.msg
return -1
return 0
# Start program
if __name__ == "__main__":
main()
When I execute it in console, it successfully connects to the target virtual machine and prints
Program executed, PID is 2036
In task manager I see process with mentioned PID, it was created by the correct user, but there is no GUI of the process (calc.exe). RMB click does not allow to "Expand" the process.
I suppose, that this process was created with special parameters, maybe in different session.
In addition, I tried to run batch file to check if it actually executes, but the answer is no, batch file does not execute.
Any help, advices, clues would be awesome.
P.S. I tried other scripts and successfully transferred a file to the VM.
P.P.S. Sorry for my English.
Update: All such processes start in session 0.
Have you tried interactiveSession ?
https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/guest/GuestAuthentication.rst
This boolean argument passed to NamePasswordAuthentication and means the following:
This is set to true if the client wants an interactive session in the guest.

Volatility plugin to extract config file from memory : Crashes after yara compile function

I am trying to write a Volatility plugin to extract configuration file used by a malware from memory dump. However, when I run this plugin (without 'sudo') without root privileges the plugin crashes at the line yara.compile. If I run this plugin with 'sudo', code after yara.compile line is not getting executed. I am not sure why yara.compile is causing this problem. Could someone help me with this? Following is the code I have written:
import volatility.plugins.common as common
import volatility.utils as utils
import volatility.win32.tasks as tasks
import volatility.debug as debug
import volatility.plugins.malware.malfind as malfind
import volatility.conf as conf
import volatility.plugins.taskmods as taskmods
try:
import yara
HAS_YARA = True
except ImportError:
HAS_YARA = False
YARA_SIGS = {
'malware_conf' : 'rule malware_conf {strings: $a = /<settings/ condition: $a}'
}
class malwarescan(taskmods.PSList):
def get_vad_base(self, task, address):
for vad in task.VadRoot.traverse():
if address >= vad.Start and address < vad.End:
return vad.Start
return None
def calculate(self):
if not HAS_YARA:
debug.error('Yara must be installed for this plugin')
print "in calculate function"
kernel_space = utils.load_as(self._config)
print "before yara compile"
rules = yara.compile(sources=YARA_SIGS)
print "after yara compile"
for process in tasks.pslist(kernel_space):
if "IEXPLORE.EXE".lower() == process.ImageFileName.lower():
scanner = malfind.VadYaraScanner(task=process, rules=rules)
for hit, address in scanner.scan():
vad_base_addr = self.get_vad_base(process, address)
yield process, address
def render_text(self, outfd, data):
for process, address in data:
outfd.write("Process: {0}, Pid: {1}\n".format(process.ImageFileName, process.UniqueProcessId))
So when I run this plugin with root privilege, I dont see the line "print 'after yara compile'" gets executed. What could be the reason? Thank you.
I installed "yara" through "pip". If you install yara through pip, you actually get yara-ctypes (https://github.com/mjdorma/yara-ctypes) which is a bit different than yara-python. So I uninstalled yara-ctypes and installed yara-python. Then it worked.

Python - create object of class from one package in different package

I started using Python few days back and I think I have a very basic question where I am stuck. Maybe I am not doing it correctly in Python so wanted some advice from the experts:
I have a config.cfg & a class test in one package lib as follows:
myProj/lib/pkg1/config.cfg
[api_config]
url = https://someapi.com/v1/
username=sumitk
myProj/lib/pkg1/test.py
class test(object):
def __init__(self, **kwargs):
config = ConfigParser.ConfigParser()
config.read('config.cfg')
print config.get('api_config', 'username')
#just printing here but will be using this as a class variable
def some other foos()..
Now I want to create an object of test in some other module in a different package
myProj/example/useTest.py
from lib.pkg1.test import test
def temp(a, b, c):
var = test()
def main():
temp("","","")
if __name__ == '__main__':
main()
Running useTest.py is giving me error:
...
print config.get('api_config', 'username')
File "C:\Python27\lib\ConfigParser.py", line 607, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'api_config'
Now if I place thie useTest.py in the same package it runs perfectly fine:
myProj/lib/pkg1/useTest.py
myProj/lib/pkg1/test.py
myProj/lib/pkg1/config.cfg
I guess there is some very basic package access concept in Python that I am not aware of or is there something I am doing wrong here?
The issue here is that you have a different working directory depending on which module is your main script. You can check the working directory by adding the following lines to the top of each script:
import os
print os.getcwd()
Because you just provide 'config.cfg' as your file name, it will attempt to find that file inside of the working directory.
To fix this, give an absolute path to your config file.
You should be able to figure out the absolute path with the following method since you know that config.cfg and test.py are in the same directory:
# inside of test.py
import os
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'config.cfg')

Categories