one server, same domain, different apps (example.com/ & example.com/tickets )? - python

I want advice on how to do the following:
On the same server, I want to have two apps. One WordPress app and one Python app. At the same time, I want the root of my domain to be a static landing page.
Url structure I want to achieve:
example.com/ => static landing page
example.com/tickets => wordpress
example.com/pythonapp => python app
I have never done something like this before and searching for solutions didn't help.
Is it even possible?
Is it better to use subdomains?
Is it better to use different servers?
How should I approach this?
Thanks in advance!

Yes, it is possible with php rewrite engine.
Internally the wordpress app may have some random URL, but you can modify it and show as whatever you wanted it to the users.
once have a look at this
https://www.addedbytes.com/blog/url-rewriting-for-beginners

It depends on the webserver you want to use. Let's go with apache as it is one of the most used web servers on the internet.
You install your wordpress installation into the /tickets subdirectory and install word-press as you normally would. This should install wordpress into the subdirectory.
Configure your Python-WSGI App with this configuration:
WSGIScriptAlias /pythonapp /var/www/path/to/my/wsgi.py

Related

Python as back-end and Polymer as the front-end

I want to create a desktop application written in python and using polymer as the front-end. To access the user interface, we use web browser such as chrome, mozilla, and safari. I did a lot of research in how to do this. The only reference I have is home Assistant, but I'm still don't quite understand about the architecture and the approach. Anyone have another solution or approach in how to do this?
python ships with a basic http server, however according to the docs it's not made for production use. But it's probably good enough for your use case (single user). On top of this you can implement a basic REST-Api and serve your frontend (.js/.html) as static content.
Or use a framework like django https://www.djangoproject.com/

How are web apps written in Python deployed?

I'm an ex-PHP developer and I'm struggling to get over the mindset that I can write an nginx config script, create the directories, fire up nginx + PHP-FPM, upload my code and let it run. I want to start playing with Python to get a feel for it and start creating web applications, but I'm a little lost as to how to get it all working.
Although some people's advice is invariably going to be to use a framework like Django, I want to see how this all works from the bottom-up.
I've come across a comparison of Python web servers, and it appears that gevent is what I'm looking for. But how does it all fit together? Do I still need nginx (except for static content) as a reverse proxy, or do I do something different? I'm facing "beginner blindness" and I'm struggling to see how it all fits together.
I am a beginner Python developer myself, so I've had the same questions. Perhaps a more advanced user can fill in the details. Here's what I've done so far:
The simplest way to get started is to simply make an executable python script (.py) and drop it in your cgi-bin. You can then access it via yourhost.com/cgi-bin/your_script.py. Simple to do, easy to use for form processing and stuff.
Some servers will require you to restart the server before it can 'see' the new .py script, which could be quite annoying for rapid development. This is one reason why a lot of people use middleware such as WSGI. Here's how I modified my Apache config to enable WSGI:
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
<VirtualHost *:80>
WSGIScriptAlias /myapp /Library/WebServer/wsgi-scripts/views.wsgi
<Directory /Library/WebServer/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
views.wsgi is simply a Python script. Now if I go to localhost/myapp/anything it will redirect to views.wsgi. It's a good idea to not put this file in your root directory, otherwise you will not be able to reference static files.
A simple app might simply look like this:
def application(environ, start_response):
start_response('200 OK', [('content-type', 'text/html')])
return ['Hello world!']
environ contains information about the path that the user is trying to reach, so the idea is that you can set up a list of URLS, and tell your program which function to call based on which URL was requested. Something like this:
path = environ.get('PATH_INFO','')
if path.startswith('/helloworld'):
# call your function that returns HTML code
I haven't dealt much with frameworks (such as Django) yet, but I think one of the advantages there is that they make it easy to fill out HTML templates using whatever variables are passed from your script. Django's template engine allows including variables as well as logic (if, for, etc) intermixed with your HTML. When the function is called, whatever it returns is sent to the client.
I'm still pretty new to all of this, so please correct me if there are any errors here...
It is little bit different with Python that with PHP.
A good thing about Python, that a common interface is defined in a standard that lets one to use various Python applications on one side and different web servers on the other. It is called WSGI, and you can read more about it here
And here is a good post about deploying Django application via ngnix.
You need some software that will execute your pyton code..
You can try a 100% python stack with tornado
Or you can use Apache, Nginx or Lighttpd (i think all written in C) and then use some mod (mod_wsgi, fgci or uWsgi) in order to make this webservers run your code using wsgi interface. This second option is like what apache+some_mod do to run your PHP code..
I have production sites running on Apache+mod_wsgi and Nginx+uWsgi

Managing subdomains

What are the best practices and solutions for managing dynamic subdomains in different technologies and frameworks? I am searching for something to implement in my Django project but those solutions that I saw, don't work. I also tried to use Apache rewrite mod to send requests from subdomain.domain.com to domain.com/subdomain but couldn't realize how to do it with Django.
UPDATE: What I need is to create virtual subdomains for my main domain using usernames from the site. So, if I have a new registered user that is called jack, when I go to jack.domain.com, it would operate make some operations. Like if I just went to domain.com/users/jack. But I don't want to create an actual subdomain for each user.
You may be able to do what you need with apache mod_rewrite.
Obviously I didn't read the question clearly enough.
As for how to do it in django: you could have some middleware that looks at the server name, and redirects according to that (or even sets a variable). You can't do it with the bare url routing system, as that only has path information, not hostname info.

Simple and effective web framework

I'm looking for a suitable cross-platform web framework (if that's the proper term). I need something that doesn't rely on knowing the server's address or the absolute path to the files. Ideally it would come with a (development) server and be widely supported.
I've already tried PHP, Django and web2py. Django had an admin panel, required too much information (like server's address or ip) and felt unpleasant to work with; PHP had chown and chmod conflicts with the server (the code couldn't access uploaded files or vice versa) and couldn't handle urls properly; web2py crashed upon compiling and the manual didn't cover that -- not to mention it required using the admin panel. Python is probably the way to go, but even the amount of different web frameworks and distributions for Python is too much for me to install and test individually.
What I need is a simple and effective cross-platform web development language that works pretty much anywhere. No useless admin panels, no fancy user interfaces, no databases (necessarily), no restrictions like users/access/levels and certainly no "Web 2.0" crap (for I hate that retronym). Just an all-powerful file and request parser.
I'm used to programming in C and other low level languages, so difficulty is not a problem.
This question is based on a complete failure to understand any of the tools you have apparently "investigated", or indeed web serving generally.
Django has an admin panel? Well, don't use it if you don't want to. There's no configuration that needs to be done there, it's for managing your data if you want.
PHP has chown problems? PHP is a language, not a framework. If you try and run something with it, you'll need to set permissions appropriately. This would be the case whatever language you use.
You want something that doesn't need to know its address or where its files are? What does that even mean? If you are setting up a webserver, it needs to know what address to respond to. Then it needs to know what code to run in response to a request. Without configuring somewhere the address and the path to the files, nothing can ever happen.
In web2py you do not need to use the admin interface. It is optional.
Here is how you create a simple app from zero:
wget http://web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
cd web2py/applications
mkdir myapp
cp -r ../welcome/* ./
Optional Edit your app
emacs controllers/default.py
emacs models/db.py
emacs views/default/index.html
...
(you can delete everything in there you do not need).
Now run web2py and try it out
cd ../..
python web2py.py -i 127.0.0.1 -p 8000 -a chooseapassword &
wget http://127.0.0.1:8000/myapp/default/index.html
When you edit controller/default.py you have a controller for example
def index():
the_input = request.vars # this is parsed from URL
return dict(a=3,b=5,c="hello")
You can return a dict (will be parsed by the view with same name as action) or a string (the actual page content). For example:
def index():
name = request.vars.name or 'anonymous'
return "hello "+name
and call
wget http://127.0.0.1:8000/myapp/default/index?name=Max
returns
'hello Max'
/myapp/default/index?name=Max calls the function index, of the controller default.py of the application in folder applications/myapp/ and passes name=Max into request.vars.name='Max'.
I think you need to be more specific about what you want to achieve, and what kind of product(s) you want to develop. A "no setup required" product may come with tons of auto-configuration bloat, while a framework requiring a small setup file could be set up in minutes, too, with much more simplicity in the long run. There is also always going to be some security and access rights to be taken into consideration, simply because the web is an open place.
Also, a framework supporting Web 2.0ish things doesn't have to be automatically a bad framework. Don't throw away good options because they also do things you don't like or need, as long as they allow you to work without them.
PHP had chown and chmod conflicts with the server (the code couldn't access uploaded files or vice versa) and couldn't handle urls properly;
PHP is not a framework in itself, it's a programming language. I don't know which PHP-based framework or product you tried but all the problems you describe are solvable, and not unique to PHP. If you like the language, maybe give it another shot. Related SO questions:
What PHP framework would you choose for a new application and why?
What’s your ‘no framework’ PHP framework?
If you need something that runs everywhere (i.e. on as many servers as possible) PHP will naturally have to be your first choice, simply because it beats every other platform in terms of cheap hosting availability.
If I were you, I wouldn't limit my options that much at this point, though. I hear a lot of good things about Django, for example. Also, the Google App engine is an interesting, scalable platform to do web work with, supporting a number of languages.
Werkzeug:
import werkzeug
#werkzeug.Request.application
def app(request):
return werkzeug.Response("Hello, World!")
werkzeug.run_simple("0.0.0.0", 4000, app)
You can optionally use werkzeug URL routing (or your own, or anything from any other framework). You can use any ORM or template engine for Python you want (including those from other Python frameworks) etc.
Basically it's just Request and Response objects built around WSGI plus some utilities. There are more similar libraries available in Python (for example webob or CherryPy).
What I need is a simple and effective cross-platform web development language that works pretty much anywhere.
Have you tried HTML?
But seriously, I think Pekka is right when he says you need to specify and clarify what you want.
Most of the features you don't want are standard modules of a web app (user and role mgmt., data binding, persistence, interfaces).
We use any or a mix of the following depending on customer requirements: perl, PHP, Flash, Moonlight, JSP, JavaScript, Java, (D/X)HTML, zk.
I am python newbie but experienced PHP developer for 12 years but I have to admit that I migrated to python because of the bottle framework. I am African so you don't have to be extra smart to use it... Give it a try, you'll love it. Hey and it also runs on appspot with no config!
Install python
Download bottle.py (single file)
Create
#your file name : index.py
from bottle import route, run
#route('/')
def index():
return 'jambo kenya! hakuna matata na bottle. hehehe'
run()
Sit back, sip cocoa and smile :)
I'd say Ruby on Rails is what you're looking for. Works anywhere, and no configuration needed. You only have it installed, install the gems you need, and off you go.
I also use ColdFusion, which is totally multi-platform, but relies on the Administrator settings for DSN configuration and stuff.
TurboGears: Everything optional.
Give bottle a try. I use it for my simple no-frills webapps. It is very intuitive and easy to work with in my experience.
Here is some sample code, and it requires just bottle.py, no other dependencies.
from bottle import route, run
#route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)
Just stumbled upon Quixote recently. Never used it though.
Use plain old ASP. IIS does not care where files are stored. All paths can be set relative from the virtual directory. That means you can include "/myproject/myfile.asp", whereas in PHP it's often done using relative paths. Global.asa then contains global configuration for the application. You hardly ever have to worry about relative paths in the code.
In PHP you'd have include(dirname(FILE) . '/../../myfile.php") which is of course fugly. The only 'solution' I found for this is making HTML files and then using SSI (server side includes).
The only downside to ASP is the availability, since it has to run on Windows. But ASP files just run, and there's not complex Linux configuration to worry about. The language VBScript is extremely simple, but you can also choose to write server side JavaScript, since you're familiar with C.
I think you need to focus on Restful web applications. Zend is a PHP based MVC framework.

Passing around urls between applications in the same project

I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the json response.
Is there a way to dynamically construct the url to App B so that I can test the code in development and not change to much before going into production? The problem is that I can't use localhost as part of a link.
I am currently using urllib, but eventually I would like to do something less hacky and better fitting with the web services REST paradigm.
You could do something like
if settings.DEBUG:
other = "localhost"
else:
other = "somehost"
and use other to build the external URL. Generally you code in DEBUG mode and deploy in non-DEBUG mode. settings.DEBUG is a 'standard' Django thing.
By "separate apps within Django" do you mean separate applications with a common settings? That is to say, two applications within the same Django site (or project)?
If so, the {% url %} tag will generate a proper absolute URL to any of the apps listed in the settings file.
If there are separate Django servers with separate settings, you have the standard internet problem of URI design. Your URI's can be consistent with only the hostname changing.
- http://localhost/some/path - development
- http://123.45.67.78/some/path - someone's laptop who's running a server for testing
- http://qa.mysite.com/some/path - QA
- http://www.mysite.com/some/path - production
You never need to provide the host information, so all of your links are <A HREF="/some/path/">.
This, generally, works out the best. You have can someone's random laptop being a test server; you can get the IP address using ifconfig.

Categories

Resources