How to install packages with python scripts - python

I want to install a package with a python script. I have read the documentation about PackageManager API (http://doc.aldebaran.com/2-4/naoqi/core/packagemanager-api.html):
So I have packaged the app with choregraphe as it is described in http://doc.aldebaran.com/2-4/naoqi/core/packagemanager.html and I have tried to install it with a python script that looks like:
import qi
import sys
if __name__ == '__main__':
ip = "11.1.11.111"
port = 9559
session = qi.Session()
try:
session.connect("tcp://" + ip + ":" + str(port))
except RuntimeError:
print ("Can't connect to Naoqi at ip \"" + ip + "\" on port " + str(port))
sys.exit(1)
service = session.service("PackageManager")
package = "C:\\test_package_handlers_01-835a92-1.0.0.pkg"
# this is to see if the problem is that python can not locate the file
with open(package) as f:
print f
service.install(package)
And here is what I receive as an error:
# provided package could be opened
<open file 'C:\\test_package_handlers_01-835a92-1.0.0.pkg', mode 'r' at 0x02886288>
Traceback (most recent call last):
File "C:/test.py", line 24, in <module>
service.install(package)
RuntimeError: C:\test_package_handlers_01-835a92-1.0.0.pkg: no such file
I guess this is because the package must be uploaded on the robot and the package file path must be the one that is on the robot.
EDITED
I have added the package to a choreographe blank project and run this blank project on the robot. This way the package was saved to the robot with path /home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg and when I have changed the path in my script ("C:\\test_package_handlers_01-835a92-1.0.0.pkg" with "/home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg") the script worked as it was intended and the package was installed on the robot.
So is there a way to install packages from my PC without uploading them to the robot, because otherwise it is better to use Choregraphe to upload projects.
Maybe it is good to give the following explanation of what I want to achieve:
I have a folder on my PC with 20 packages for example
I want to install all those 20 packages with one python script
There is a python script that installs all the packages from the folder when it is invoked like this:
python package_installer.py path_to_packages_folder
EDITED_2
import qi
import ftplib
import os
ROBOT_URL = "10.80.129.90"
print "Uploading PKG"
pkg_file = "my-application-0.0.1.pkg"
pkg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), pkg_file)
ftp = ftplib.FTP(ROBOT_URL)
ftp.login("nao", "nao")
with open(pkg_path) as pkg:
ftp.storbinary("STOR "+pkg_file, pkg)
print "Connecting NAOqi session"
app = qi.Application(url='tcp://'+ROBOT_URL+':9559')
app.start()
session = app.session
print "Installing app"
packagemgr = session.service("PackageManager")
packagemgr.install("/home/nao/"+pkg_file)
print "Cleaning robot"
ftp.delete(pkg_file)
ftp.quit()
print "End"
app.stop()
This piece of code ftp = ftplib.FTP(ROBOT_URL) throws the following exception:
Traceback (most recent call last):
File "C:/Stefan/DSK_PEPPER_clode_2/PythonScripts/_local_testing/uploading_and_installing_package.py", line 11, in <module>
ftp = ftplib.FTP(ROBOT_URL)
File "C:\Python27\lib\ftplib.py", line 120, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 575, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Also when I connect to the robot with username 'nao' and pass 'nao' as described in http://doc.aldebaran.com/2-5/dev/tools/opennao.html and then try to create a folder in /home/nao/.local/share/PackageManager/apps/ with sudo mkdir it informs me that: Sorry, user nao is not allowed to execute '/bin/mkdir dasdas' as root on Pepper.. If I use only mkdir here is what it tells me: mkdir: cannot create directory 'new_folder': Permission denied

Using qibuild, you can also directly install using:
qipkg deploy-package /path/to/my-package.pkg --url nao#10.10.23.45

You indeed need to upload the file before. You can use scp or sftp to do this. Once the .pkg is on the robot then you can use PackageManager.install.
Imagine something like:
import qi
import paramiko
import os
ROBOT_URL = "10.80.129.90"
print "Uploading PKG"
pkg_file = "my-application-0.0.1.pkg"
pkg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), pkg_file)
transport = paramiko.Transport((ROBOT_URL, 22))
transport.connect(username="nao", password="nao")
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(pkg_path, pkg_file)
print "Connecting NAOqi session"
app = qi.Application(url='tcp://'+ROBOT_URL+':9559')
app.start()
session = app.session
print "Installing app"
packagemgr = session.service("PackageManager")
packagemgr.install("/home/nao/"+pkg_file)
print "Cleaning robot"
sftp.remove(pkg_file)
sftp.close()
transport.close()
print "End"
app.stop()

Related

SSL hostname.bundle error in python script

we have a python script running on a centos web panel machine designed to start up a Git interface
this was working fine till the hostname ssl expired yesterday
we replaced the hostname.cert and hostname.key and hostname.bundle with a new wildcard digicert ssl and restarted all services but get the error:
Traceback (most recent call last):
File "/home/hosting/scripts/git-web-interface/server.py", line 12, in <module>
certificate_chain = "/etc/pki/tls/certs/hostname.bundle"
File "/usr/local/lib/python3.6/site-packages/cheroot/ssl/builtin.py", line 101, in __init__
self.context.load_cert_chain(certificate, private_key)
FileNotFoundError: [Errno 2] No such file or directory
the file being executed is:
# Our stuff
import customGlobals
# Web.py
import web
if __name__ == "__main__":
from cheroot.server import HTTPServer
from cheroot.ssl.builtin import BuiltinSSLAdapter
HTTPServer.ssl_adapter = BuiltinSSLAdapter(
certificate = "/etc/pki/tls/certs/hostname.cert",
private_key = "/etc/pki/tls/private/hostname.key",
certificate_chain = "/etc/pki/tls/certs/hostname.bundle"
)
customGlobals.app.run()
we have triple checked that the cert, key and chain paths are correct and the files exist with the right ownership but its saying the hostname.bundle is incorrect
anyone know what's going on here?
the hostname.bundle appears to be just a copy of the cert file, so im guessing that's not right but i don't know what it should be if its not that?
can anyone advise?

psutil.AccessDenied when using StanfordCoreNLP in Pycharm? [duplicate]

This question already has answers here:
psutil.AccessDenied Error while trying to load StanfordCoreNLP
(3 answers)
Closed 2 years ago.
# coding=utf-8
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP(r'/Users/silas/stanford-corenlp/', lang='zh')
sentence = '清华大学位于北京。'
print nlp.word_tokenize(sentence)
print nlp.pos_tag(sentence)
print nlp.ner(sentence)
print nlp.parse(sentence)
print nlp.dependency_parse(sentence)
nlp.close()
I'm using Mac. Java, NLKT, and Stanforcorenlp toolkit are all ready. When I'm testing the project, the error came out.
Traceback (most recent call last):
line 5, in <module>
nlp = StanfordCoreNLP(r'/Users/silas/stanford-corenlp/', lang='zh')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/stanfordcorenlp/corenlp.py", line 79, in __init__
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/__init__.py", line 2120, in net_connections
return _psplatform.net_connections(kind)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/_psosx.py", line 255, in net_connections
cons = Process(pid).connections(kind)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil/_psosx.py", line 347, in wrapper
raise AccessDenied(self.pid, self._name)
psutil._exceptions.AccessDenied: psutil.AccessDenied (pid=25422)
I guess it is because the Pycharm project isn't running under a root user. But how can I configure the IDE to fix the problem?
Unfortunately, if you look at the psutil project in _psosx.py, under net_connections, line 243 says..
Note: on macOS this will fail with AccessDenied unless the process is owned by root.
That means that you'll need to run as root by doing something like sudo pycharm.sh.
If you don't want to run your entire IDE as root, there's a few examples on SO on how you can run a specific script with super-user privileges. For instance see Debugging in pyCharm with sudo privileges.
This problem seems to be specific to Mac OS X which would not allow Python to check the current port.
Comment this portion of code of corenlp.py file:
if self.port is None:
for port_candidate in range(9000, 65535):
if port_candidate not in [conn.laddr[1] for conn in psutil.net_connections()]:
self.port = port_candidate
break
if self.port in [conn.laddr[1] for conn in psutil.net_connections()]:
raise IOError('Port ' + str(self.port) + ' is already in use.')
Replace by this line:
self.port = 9999
Source: https://github.com/Lynten/stanford-corenlp/issues/26#issuecomment-445507811
Another solution is to run StanfordCoreNLP with a sudo command line.

Cambrionix PowerPad15S is not showing connected devices in Ubuntu

I am using Cambrionix PowerPad15s for my devices but while running their first code which is to find all the device connected to the usb i am having some issue in jsonrpc file(Which is provided by the company itself).
I have to import this-
from cbrxapi import cbrxapi
This code is to get all the connected device in the usb port and save in result variable-
result = cbrxapi.cbrx_discover("local")
Rest of the code is-
if result==False:
print "No Cambrionix unit found."
sys.exit(0)
unitId = result[0]
handle = cbrxapi.cbrx_connection_open(unitId)
nrOfPorts = cbrxapi.cbrx_connection_get(handle, "nrOfPorts")
cbrxapi.cbrx_connection_close(handle)
print "The Cambrionix unit " + unitId + " has " + str(nrOfPorts) + " ports."
The error I am facing in is
Traceback (most recent call last):
File "cbrx_api_quickstart.py", line 9, in
result = cbrxapi.cbrx_discover("local")
File "/usr/local/share/cbrxapi/jsonrpc-0.1/jsonrpc.py", line 936, in call
return self.__req(self.__name, args, kwargs)
File "/usr/local/share/cbrxapi/jsonrpc-0.1/jsonrpc.py", line 908, in __req
raise RPCTransportError(err)
jsonrpc.RPCTransportError: [Errno 111] Connection refused
The product I am using is Cambrionix
Sorry for not explaining properly. I am still in learning phase..
Found the solution-
I have to install one more file to my system to get the code working..
$ sudo apt-get install avahi-daemon
And I need to ensure that one more script is running on my system.
install_service.sh in /usr/local/share/cbrxd/setup

Using nmap library in Python program

I have a simple Python program which uses nmap library to do port scanning.
from optparse import OptionParser
import nmap
from threading import *
screenLock=Semaphore(value=1)
def nmapScan(thost,tport):
x=nmap.PortScanner()
x.scan(thost,tport)
state=x[thost]['tcp'][int(tport)]['state']
print "[*]" + thost + "tcp/"+tport+" "+state
def main():
parser=OptionParser('usage %prog -H <target host> -p <target port>')
parser.add_option('-H',dest='thost',type='string',help='specify target host')
parser.add_option('-p',dest='tports',type='string',help='specify target port[s] seperated by comma')
(options,args)=parser.parse_args()
thost=options.thost
tports=options.tports
tports=tports.split(',')
if (thost==None)|(tports==None):
print parser.usage
exit(0)
for i in tports:
nmapScan(thost,i)
main()
When i run the program, i get the following error.
akshayrajmacbookpro$ python nmapScanner.py -H 192.168.1.60 -p 80,443
Traceback (most recent call last):
File "nmapScanner.py", line 28, in <module>
main()
File "nmapScanner.py", line 26, in main
nmapScan(thost,i)
File "nmapScanner.py", line 10, in nmapScan
state=x[thost]['tcp'][int(tport)]['state']
File "build/bdist.macosx-10.11-intel/egg/nmap/nmap.py", line 555, in __getitem__
KeyError: '192.168.1.60'
I tried using url instead of ip in the command line. But I get the same error. Being new to Python, I am not able to understand and resolve this.
x (instance of nmap.PortScanner) does not contain those keys. To be able to iterate the scan results you can do this:
for host, result in x._scan_result['scan'].items():
print "[*]" + thost + "tcp/" + tport + " " + result['status']['state']
It's best if you looked at the docs or source code of python-nmap to see what other useful info is available e.g. Service name and version that is listening on that port.
More info here: https://bitbucket.org/xael/python-nmap/src/f368486a2cf12ce2bf3d5978614586e89c49c417/nmap/nmap.py?at=default&fileviewer=file-view-default#nmap.py-381
The host does not exist in the result of the scan as a "Key" for the Dictionary that forms a part of the data thrown up by scan data. That is probably, in my opinion, the reason for the error. Thanks

Pycharm python console socket.gaierror

I'm running Pycharm 4.5.3 on OS X Yosemite (10.10.3). I created a simple python program, and tried opening the python console, and got this stack trace error:
/usr/bin/python -u /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 59286 59287
Error starting server with host: localhost, port: 59286, client_port: 59287
Unhandled exception in thread started by <function start_server at 0x100d9bd70>
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 283, in start_server
server = XMLRPCServer((host, port), logRequests=False, allow_none=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleXMLRPCServer.py", line 593, in __init__
SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Couldn't connect to console process.
A similar question was raised here on stack overflow, but the root of the error was the string localhost passed in with white spaces, which is not the case here (host has been assigned to 'localhost'). Does anyone have any idea? This isn't really a big deal, seeing as I can use python command line in terminal, but I'm curious if this is a bug within Pycharm.
Edit: here's the source code to the Pycharm script.
if __name__ == '__main__':
import pydevconsole
sys.stdin = pydevconsole.BaseStdIn()
port, client_port = sys.argv[1:3]
import pydev_localhost
if int(port) == 0 and int(client_port) == 0:
(h, p) = pydev_localhost.get_socket_name()
client_port = p
pydevconsole.StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))
The answers given so far do not get at the underlying cause, which is likely to be that your OS X /etc/hosts file does not contain an entry for: 127.0.0.1 localhost
Update the hosts file with the line:
127.0.0.1 localhost
(you will need to use sudo), then restart pyCharm. Unless you know what you are doing, editing your IDE source code is not a good idea.
I'm new in python, stuck on the same problem for half a day. Finally solved this problem by setting the host to 127.0.0.1.
If you have the same issue you can do this by:
open pydevconsole.py in pycharm, navigate to the main script part and find this line:
pydevconsole.StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))
ctrl + click the function get_localhost() to navigate to its source:
_cache = None def get_localhost():'''
Should return 127.0.0.1 in ipv4 and ::1 in ipv6
localhost is not used because on windows vista/windows 7, there can be issues where the resolving doesn't work
properly and takes a lot of time (had this issue on the pyunit server).
Using the IP directly solves the problem.
'''
#TODO: Needs better investigation!
global _cache
if _cache is None:
try:
for addr_info in socket.getaddrinfo("localhost", 80, 0, 0, socket.SOL_TCP):
config = addr_info[4]
if config[0] == '127.0.0.1':
_cache = '127.0.0.1'
return _cache
except:
#Ok, some versions of Python don't have getaddrinfo or SOL_TCP... Just consider it 127.0.0.1 in this case.
_cache = '127.0.0.1'
else:
_cache = 'localhost'
return _cache
I believe the problem is caused by the returning "localhost" of this function, make it "127.0.0.1" solved the problem.
I had this issue on OS X El Capitan, running Pycharm 2016.2.
In any editor open: /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py
If you're navigating via Finder, right-click the Pycharm.app file and choose "Show Package Contents" to get access to the path.
Find the line:
pydevconsole.start_server(pydev_localhost.get_localhost(), int(port), int(client_port))
Change it to:
pydevconsole.start_server('127.0.0.1', int(port), int(client_port))
Restart Pycharm and choose Tools->Python Console...
You should then see:
Users/.../env/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 56582 56583
PyDev console: starting.
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/...'])
Python 3.5.0 (default, Dec 1 2015, 12:50:23)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin

Categories