Migrating an Asterisk interaction program to a web app from a command line python script
I am trying to create a small web application to interact with the Asterisk telephony platform. There is a lot of similiar stuff out there but I want the learning experience of making my own.
Here is what I have now: A simple python script that allows someone to interact with the manager interface via the command line. Basically how the script works is: 1) Connects to the Asterisk Manager Interface. 2) Gets two phone numbers from the user on the CLI. 3) Originates the two calls 4) Brings the originated calls into a common meetme conference. 5) A 'while' loop waits for input from the user. Only characters a-z are allowed. 6) "a-z" correspond to .WAV files that are played or "injected" into the conference by the use of originating another local extension.
To me it is kinda neat and now I want to do the same exact thing from a web app but I don't know where to start. What languages would I need to use? I know basic html and have played with javascript. What techniques would be required to pass form input variables to the python script on the server (web server and asterisk server are the same machine)? CGI? What about using PHP? I will probably need to learn that at somepoint... If I use PHP can I still "call" the python script and pass values to it? I was hoping to use javascript eventually to allow the user to play a sound that corresponds to a button (just like a character in the command line version) without hitting submit but plan on just getting it to work for now. I guess this question is also turning into "Should I just learn PHP and drop the python?" I feel that I could really add to my current telephony skill-set (and have fun... this Asterisk stuff is a blast) by being able to build web based tools that interact with Asterisk (maybe Freeswitch too).
Any hints or guidance would be greatly ap开发者_如何学JAVApreciated.
I'd recommend just wrapping the code you have in a wsgi interface, which is python standard for web services (something like cgi but with functions instead of programs, so it has simpler interface and can be precompiled in the web server).
The WSGI web contains links to many python web application frameworks too, so look at them and pick one. Django, Pylons or TurboGears seem to be the more popular ones and Bottle is great for quickly putting together smaller web applications. You might eventually want to also have a look at Google App Engine, which provides somewhat restricted python environment very fast hosting.
Generally python has great web frameworks, is usually faster than PHP and you can use the same skills on and off the web, while PHP is mostly useless outside of web. Also with python, you won't learn to mix HTML with code (as in PHP), which you'd have to unlearn if you ever get to work together with a designer.
You could do everything from PHP if you wanted to. But honestly, you've written the script in Python already, why not just use Python for the web stuff too? Django is one of the most popular web frameworks around. Very easy to learn if you follow the tutorial.
Django may be a little heavy weight for what you want to do, but I'd still highly recommend using it.
On the server that you have Asterix running on, configure Apache web server with mod_wsgi so that it can run Python. Have your Django contain a form that is posted to a view. That view can then execute code from your Asterix script.
The major problem that you're going to have is that the Web is not stateful. This means that while you can call a function quite easily, maintaining an interactive session can be quite hard. Your while
loop will be difficult to maintain across a HTTP session. You can use something like commet to maintain an open session, but I've not done this before and I don't know how easy it will be to do.
I would try for something simple first. Have a python script such as this:
# asterix.py
def make_call(target, source):
conn = connect_to_asterix()
conn.make_call(target, source)
And then inside your django view:
# views.py
def make_call(request):
if request.method == 'POST':
form = AsterixForm(request, request.POST)
if form.is_valid():
import asterix
asterix.make_call(form['target'], form['source'])
This will make a call from source to target, and then will stop processing. You won't be able to interact with the call after that (unless you return an ID which will allow you to manipulate that call further within asterix), but it will still be a solid place to start.
精彩评论