How to use Asterisk AGI with python3? - python-3.x

Using Asterisk 16.2.1 my AGI script (at the bottom) works with python2 #!/usr/bin/env python2, but not with python3 #!/usr/bin/env python3.
I do not even get as far as agi.verbose("python agi started") (with python3), so I assume it has something to do with the AGI import or initialization agi = AGI()
Having used agi set debug on does not really help, the only info I see is
Launched AGI Script /home/.../asteriskAgi.py
-- <SIP/..-00000002>AGI Script /home/.../asteriskAgi.py completed, returning 0
As it works with python2, but not 3 I have also installed pyst3 from https://pypi.org/project/pyst3/ , but it did not help (it does not work with or without pyst3 installed).
Q: Any idea how to configure asterisk for python3, or how to find the root cause?
Any chance to get more detailed log information of where the script actually fails_
#!/usr/bin/env python3
import sys
import rpyc
from asterisk.agi import AGI
agi = AGI()
agi.verbose("python agi started")
aCallerId = agi.env['agi_callerid']
aType = agi.env["agi_type"]
agi.verbose("XXXXXXXXXXXXXX call from %s" % aCallerId)
agi.verbose(sys.executable)
l = [aCallerId, aType]
agi.verbose("XXXXXXXXXXXXXX l")
c = rpyc.connect("localhost", 18861)
c.root.asteriskCall(l)
Even this minimalistic version does not work with "3"
#!/usr/bin/env python3
import rpyc
from asterisk.agi import AGI
agi = AGI()
agi.verbose("python agi started")
eventually solved by:
uninstalled pyst3 and
forced a re-install of pyst2 like pip3 install --upgrade --force-reinstall pyst2. No idea what went wrong in the first place.

Your minimalistic version works for me(with pyst2 installed via pip)
Check permission and installed packages. Also ensure that your asterisk running under environment which able find python3 and packages installed.

Related

Scapy not working with Python 3.6. Import problems with Python?

I am trying to test a simple code that will inspect a .pcap file using scapy. My OS is Ubuntu 18.04 and my Python version is 3.6.
However when I do: from scapy.all import * I get the error message ModuleNotFoundError: No module named 'scapy.all'.
When I change this to be from scapy import *, I get an error later in my code when I try and use the scapy sendrecv sniff function. I get the error NameError: name 'sniff' is not defined. Note if I switch from sniff() to rdpcap() I get the same error but its now "rdpcap is not defined".
I've read through a bunch of previous stack overflow answers and nothing is helping. My python script name is "pcap_grapher.py", so the issue is not the script name being scapy.py.
When I run pip3 install scapy in the terminal I get the message Requirement already satisfied: scapy in /home/vic/.local/lib/python3.6/site-packages (2.4.4)
I've also tried running pip3 install --pre scapy[basic] as the docs recommend here: https://scapy.readthedocs.io/en/latest/installation.html but it didn't help.
Any advice would be greatly appreciated. I am super stuck.
First off, you will never be able to import sniff() from scapy, since it is within the submodule scapy.all. There are two ways I can think of to fix this:
Since you're on Ubuntu, try running sudo apt-get install python3-scapy and see if it lets you run from scapy.all import sniff now.
Check the path that python is currently pulling from by typing the following in terminal:
$ which scapy
/location-of-scapy-on-your-system
Then run
$ python
>>> import os
>>> scapy_path = "/location-of-scapy-on-your-system"
>>> if not scapy_path in os.sys.path: os.sys.path.append(scapy_path)
That should hopefully fix things. If not, it's possible you installed the wrong version of scapy: you can check this by running
$ python
>>> import scapy
>>> print(scapy.__version__)

ImportError: No module named flask using MAC VSCODE

ScreenGrab of error when trying to import db I created
You either need to install Flask from pip, or something similar, or Python is not able to find the location of Flask if it is, in fact, installed.
At your Python prompt, try this to see where it is looking for libraries:
>> import sys
>> print (sys.path)

PIP installed pywin32 service fails: Incorrect function

Running Python 3.6.5, PyWin32 223.
After install I have included the the C:\Python32\lib\win32 folder in path.
I am just running the normal testService shell that seems to be all over the internet. Can be found below.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
I can "compile" and install the service fine, as soon as I run it I get an error to check the system logs. When I do I get the following error.
The Test Service Process service terminated with the service-specific error Incorrect function..
I have not been able to find any help through google from this decade. I am new to the library and the only help I found was to add its lib to the path. If its a path error, its no longer that. Anyone have any ideas?
Thanks in advance!
It turns out that it was a permission issue. I request root for 30 seconds and worked like a charm. Just FYI if anyone is having this problem.
In my case the problem was in the way I run the python module. Instead of executing the Python script, I use
$ python -m module
This works correctly when running in the appropriate directory but when running as a service, the module could not be found. So the solution if executing modules directly with Python is to pip install the module so that it can be found by the service.
In my case was due to the fact that the python script I was trying to run as a service wasn't in the same drive of the python installation. Also check this useful answer.

virtualenv can't find modules

So I had been working with virtualenvs a couple months ago but had to stop for a while and now I don't seem to be able to get it working again.
Here is what I did
D:\CS\Python_Projects\HomeCenter>venv\Scripts\activate
(venv) D:\CS\Python_Projects\HomeCenter>pip freeze
You are using pip version 6.0.8, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Flask==0.10.1
Flask-SQLAlchemy==2.0
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.8
waitress==0.8.9
Werkzeug==0.10.4
(venv) D:\CS\Python_Projects\HomeCenter>server.py
Traceback (most recent call last):
File "D:\CS\Python_Projects\HomeCenter\server.py", line 3, in <module>
from waitress import serve
ImportError: No module named 'waitress'
(venv) D:\CS\Python_Projects\HomeCenter>
Same happens if I were to run app.py, it won't find flask. I have been using PyCharm and when I check there it tells me the interpreter is pointing towards 3.4.2 virtualenv at D:\CS\Python_Projects\HomeCenter\venv
So why can't it find the modules?
Oh contents of server.py in case it's relevant
from waitress import serve
import os
from app import app
port = int(os.environ.get('PORT', 9999))
serve(app, host='0.0.0.0', port=port)
Hello I am korean student so I can't English not well. Please understand.
In my case, I have a well operated program but suddenly occured "not found" error like you after update mac os to BigSur.
but I finally found solution.
when before update I compiled by "source venv/fileName.py" but after update I compile by "python3 venv/fileName.py" then well operate
You try it too.
A couple of things could have happened here:
Use which python and which pip to check if both are from the virtual env itself.
As dirn suggested in the comments, it might be a PATH mixup. The packages were installed in the virtual environment but the script was executed from outside the environment interpreter. (Use venv\Scripts\python.exe <filename.py> instead of just filename to execute).

How to find the Django package

I work with ubuntu 12.04 and I want to know whether Django is currently installed. I tried it like this at first:
$ python
>>> import django
>>> django
but I want to do it in the terminal. I don't want to use dpkg because the user might have installed it with python-pip. I already tried find and whereis but that didn't work either.
Write simple script using python:
#!/usr/bin/python
import sys
import os
try:
import django
print "found"
print "path={0}".format(os.path.dirname(django.__file__))
sys.exit(0)
except ImportError:
print "not found"
sys.exit(0)
then run script and check for exit code
$ python script.py
$ echo $?
You should have a look at this question: [How do I find the location of Python module sources?][1]

Resources