Running Django Rest Framework inside Apache - python

I have a web server running Apache, and I need to implement a RESTful API on the same domain, and I'd like to use Django Restful Framework to serve the REST calls.
For example: going to http://myawesomedomain.com/ in a browser serves a good old fashioned web page delivered by Apache, but I need requests to http://myawesomedomain.com/api/customers/... to be handled by my Django Restful application.
Can someone please point me in the right direction. Is there an apache mod I need to activate to get it to serve Python? Do I have to redirect those requests to another service on the server?
Not looking for a comprehensive tutorial. I just don't know where to start.
Thanks in advance

I did some more digging and found the answer myself.
You use mod_wsgi.
Here is a perfect tutorial to get started: https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/modwsgi/

Related

Dispatching /Mapping URLS in python without using Flask

Back ground:
I bought a web hosting plan and created (by WordPress) a website. Now I want to create a new website on that server by python/flask but not WordPress because the standardized functions do not meet my requirements.
Problem description:
For testing purpose, I did not edit any Conf files or WSGI settings, just executed "python init.py" to start the new python web applications, in the backend, it showed app is running at http://:localhost:5000. But in the frontend, the web browser ended up waiting for response.
My web hosting provider had a look and they said Flask is not allowed for some security reasons.
My question:
I need to bind URLs to proper pages without using flask framework. Any suggestions and recommendations are welcome. Thank you very much for your help.

openshift is looking for 'wsgi' application, I don't want 'wsgi'

I don't know if you know about this website (as I can't read your mind), but openshift is it. It is a web-hosting website. You can use python or whatever for server-side.
The problem is openshift is looking for a wsgi application. But I am using websockets with tornado so I can't use wsgi. How does I make openshift not look for wsgi, but any type of applicaiton? Or does the website only support that.
The error message I am seeing is:
'wsgi.py' does not contain WSGI application 'application'
I really need a server with websockets, I am planning on creating a multiplayer online game with javascript using websockets.
Thanks for the help, everyone.
Put your application in app.py. This will allow you to run whatever you want. See:
http://blog.dscpl.com.au/2015/08/running-async-web-applications-under.html

Deploying Python on AWS Elastic Beanstalk

I'm coming from a php background and now I'd like to host a Python web application on AWS but have a few questions about it.
Do I need to use a framework like Django/Flask? Is there anyway I can use core Python to do the same? Back in php, although I tried learning Laravel in the middle, I was able to just use core php with apache and host up my website.
Tried deploying my Python code on my AWS but got an error that application.py was missing. I'd like to know, what exactly is application.py and what is it supposed to contain? Similarly, what's wsgi.py I see here and there. Are these actual files I need to create for the web app to be hosted? Is there some specific code that has to go into them or are the pre-created files by frameworks like Django, etc? Because I could hardly find too much information on them online.
I had recently tried following this tutorial from the AWS official site but to no luck.
The reason for being reluctant to use Django is the shortage of time to learn it. But if it were to make the task of hosting a Python web app easier, I would definitely look at it.
And how is the version of Python set? Because the Python codes I've written use the python3 libraries for BeautifulSoup and urllib.
I have read a lot of articles on the web but the first thing I get on searching for Python on the web or with AWS, is Django or Flask or something. How exactly does it work? When it came to php, it was simple copying the files into the /www/ folder of the server machine and I could access the website via it's url. Maybe I've read too many posts to put them all together so could someone please set it straight for me? It would be greatly appreciated!
Thanks a lot!
You will waste a lot more time trying to write a WSGI application yourself from scratch. Use a framework, it will save you a lot of time.
PHP is very different to WSGI and WSGI sits well below the level of functionality that PHP provides out of the box. PHP is more like what frameworks in Python provide. So go learn a framework. If Django seems too complicated, try Flask first.
Also don't try and do it on AWS from the outset, learn the frameworks by using the development servers they provide on your own box. Just work through their respective tutorials.
Actually AWS Elastic Beanstalk give you pure Python (2.6/2.7/3.4) with Apache + mod_wsgi as web proxy. You can look all supported Python environment in here. So, you can use any Python web framework (such as Django, Flask, etc.) in your web app. If you can, choose common and supported framework by AWS (Django/Flask).
You can think: Python + Apache + mod_wsgi is equivalent to PHP + Apache + mod_php.
Please take a look into AWS Elastic Beanstalk documentation for how to working with Python here. Read the Deploying a Django Application or Deploying a Flask Application if you choose one of them. You need to provide what Elastic Beanstalk environment (mod_wsgi) needs.
Same as PHP, Python actually only copy and paste the files. If you want to make Python web app without framework, you need to follow the WSGI standard. You can take look into this question. In my opinion, better if you use a framework, because it handles the WSGI part for you.
First of all this is a good blog post to start from if you are using Django
I don't know much about Flask, with Django once you understand the core concepts it's not hard at all.
application.py is the file that aws looks for as stated in the blog post I pointed to:
By default eb assumes our wsgi file is called application.py
this can be changed to your local wcgi.py file that Django makes when you start your project with django.
Beware that you want to use your static url correctly so aws will read them from the right folder. I personally disagree about the way the static files configuration in the post.
It's better to stay with the aws default which is "static" and just set static url in django settings to "/static/"

Amazon AWS - Python webframework - dynamodb

I've done a lot of research for Google App engine. I ended up with webapp2. Now we realized that AWS has many advantages over GAE for our project.
I feel a little bit overwhelmed because there is so much information about AWS.
I ended up with the nosql dynamoDB solution.
Now I really love lightweight frameworks like webapp2 , its really simple and easy to use. It also don't hide post and get from you.
Now does it makes sense to use webapp2 for AWS ? Mabye there other frameworks that would fit better for our project.
(We want to go in a filehosting direction)
I mean there are literally 50 frameworks for python. And I didn't find a chart with pro/cons.
The last frameworks that I investigated are : web2py , pylon(now pyramids?) and tornado.
To be honest I am really confused.
So if I want to have a lightweight framework for AWS would you recommend me to stay with webapp2?
Resources:
Amazon AWS web framework for Python
Edit #1
Now it's a decision between tornado and webapp2. considering that I want to use dynamoDB.
Tornado is a fast non-blocking web server itself. It's easy to write simple apps in a few minutes, but it has nothing AFAIK like templates, views, etcetera. If you want to serve files it would be very quick to connect a tornado server together with boto (https://github.com/boto/boto) and use dynamoDB or S3.
On the other hand, pylons (now pyrmaid) and we2py are full web application frameworks with no web server. In fact, they come with a lightweight development server (at least pylons), but if you want to put up a site in AWS you should use something like nginx (http://nginx.org/), apache or some other wsgi server (http://wsgi.readthedocs.org/).
If you want to go for a lightweight framework and use python, I'd go for web2py, easy to configure and build apps. (A curious note: it used to be a single .py file) you can try it online: http://www.web2py.com/demo_admin/default/site
Regarding webapp2 I've never used it, but I've heard it's similar to Appengine web framework. So if you're comfortable with it stay there.
Either direction, boto is the python interface for AWS so if you choose python, you'll have to check it out. It is actively mantained.

REST web service in python 3?

I'm new to the python world and I'm currently in a new project using it. So since we we're there to learn, we chose to start with python 3. Now, we need to make a RESTful web service. After reading a few, I found out that the most used framework for web services is Django... and I also read on the Django website that it does not yet supports python 3.
Is there any other API that I could use in order to make a RESTful web service?
Thank you.
CherryPy has a Python 3 branch. Not sure how advanced it is, but if Fu-manchu passes by this question, he will be able to tell you.
The Bottle framework claims to work with Python 3. You might inquire on their google group for details.
I really like to use bottle and run it on cherrypy. This is really easy, do everything per bottle instructions, and then in the run line, tell it to use cherrypy as it's server.
Exampple:
app.run(host='localhost',
port=8080,
server='cherrypy',
reloader=True,
debug=True)

Categories

Resources