Web.py routing when added nginx as upstream - python

I've been writing a client side app and a server side app as two separate apps and I want the client to use the server. The client is written in javascript and the server is written in python using web.py as the engine to deliver to the client. The client and server must be on the same web domain.
The server part has a route defined as:
'/data/(.*)', 'applicationserver.routes.Data.Data'
This works fine running it locally using http://buildserver/data/transform
I'm setting it up as a site in nginx like this:
upstream app {
server 127.0.0.1:8081
}
and adding it to the web application like this:
location /server {
...
proxy_pass
}
The new path to the route would be ` but for obvious reasons this will not work as the server app is listening for/dataand not/server/data`.
I have tried to change the route in python to (.*)/data/(.*) which sort of works except that it throws the error:
<type 'exceptions.TypeError'> at /data/transform
GET() takes exactly 2 arguments (3 given)

I figured out what was going on just before posting but I hope I can help someone else by posting this anyway.
web.py is sending the matched group to the GET and therefor using (.*)/data/(.*) sent both the start and the end of the path to the GET which is why it failed.
Setting the route to .*/data/(.*) gave me what I was after and only sent the part after data to the GET function.

Related

How to develop in production and development mode of Flask website

This is not a pure technical issue but more methodologic question.
I've seen Q&A regarding configuration for DEBUG and PRODUCTION ENVs but my question is concerning other issue.
When I started working on the project on my local machine I edited the hosts file to redirect www.example.com (I used the same URL for my live website) to 127.0.0.1 as I used to and it's working great.
Now when www.example.com is live, I wanted to know what is the right configuration for keep developing the website?
The only idea I came up with is to use www.example.org (So I won't lose actual access to www.example.com) in my hosts file and on the code to use IF DEBUG to redirect traffic to example.org instead of example.com but I feel there are better options.
I also would love some tips about the right way of working with git to post local updates to the live server.
When I want to access the website I'm running locally I just use http://127.0.0.1:5000 in my browser to access it.
If you've hardwired the domain "www.example.com" into your flask logic somewhere, i.e. when passing a redirect link to an OAuth service I would consider removing that hardcoded logic. Instead use an environment variable which you set differently on production/dev or else access to the current domain of a request with request.url_root or request.headers['Host'].
Make use of Flask's SERVER_NAME and PREFERRED_URL_SCHEME builtin configuration values.
class Config(object):
# blah blah
class DevelopmentConfig(Config):
SERVER_NAME = "example.local"
PREFERRED_URL_SCHEME = 'http'
class ProductionConfig(Config):
SERVER_NAME = "example.com"
PREFERRED_URL_SCHEME = 'https'
On your development machine map example.local to 127.0.0.1 .

How to run node and django server at a time

I am making a video chatting application in which I will use web sockets from npm and I have written WebSocket related code in a server.js file like this
var webSocketServ = require('ws').Server;
var wss = new webSocketServ({
port: 8000
})
var users = {};
var otherUser;
wss.on('connection', function (conn) {
console.log("User connected");
conn.on('message', function (message) {
var data;
.
.
.
and so on
and for managing URLs I used Django,
so my problem is when I use python manage.py runserver, server.js is not running and application is not connecting to the server and if I run "node server.js" application is connecting to server but I am unable to manage URL as in Django code
so I opened two instances of terminals and ran node in one terminal and python in another but I know its not the correct way and it won't be efficient while hosting
is there any way to run both servers at a time?
If you need the two servers to appear to run on the same port, you'll need to set up one to proxy certain requests to the other. (I'd recommend having the Node.js server proxy everything non-websocket to Django using e.g. https://github.com/http-party/node-http-proxy).
If you don't need the two servers to run on the same port, just change them to use different ports, in which case you'll access the two contents on different URLs. You can change that port: 8000 in the Node app, or in Django's runserver, do e.g. runserver 127.0.0.1:8010 (or 0.0.0.0:8010 to expose the dev server beyond your own machine).

Python Flask can't host Backend on my machine

So I wanted to host my backed on my machine for my website.
The Problem is I can't the frontend works perfectly but the Flask backend doesn't.
The backend works when I use localhost or my local ip to access it.
But doesn't work when I try to use it with the link :
https://desktop-1234567.1234567890qwert.myfritz.net:3182
(this one is fake)
It displays this error message :
So basically it says that it can't find the backend the Flask backend also dosen't react.
Even though I port forwarded the port 3182 with my-Fritz :
I also turned off Windows Firewall and it dosen't help
My Fritz is basically a free service from avm for Firtz-Box users (that's my Router)
It generates a sub domain where all your traffic is routed through this is useful since this url is static.
I tried it with a simpler backend :
from flask import Flask
app = Flask(__name__)
#app.route("/test")
def test():
return "<h1>Test</h1>"
if __name__ == "__main__":
app.run(
debug=False,
host='0.0.0.0',
port=3182,
threaded=True
)
and it also doesn't work it gives the same error message as above.
I also tried using waitress to host this simple app but it also won't work.
It has to be Flask since I can access my Frontend with the link on port 80 and 443.
I'm on Windows 10 and I hope somebody knows why this isn't working.
So yeah pleas help me I searched and it appears that no one had this exact problem before (But you never know on Stack Overflow)
Well I found out what the issue is.
The Issue is that I used "0.0.0.0" as the host parameter to run the app.
This tells flask to use IPV4, however I looked in to it and found out that "My Fritz" uses IPV6 so I just had to change the host parameter to use IPV6 by replacing "0.0.0.0" with ::.
That's it hope it helps, if anyone runs into the same issue as me.

How to run two django apps on same dev machine

I'm trying to run two separate django apps that need to communicate with each other using a restfull api. In the real life there would be two separate machines but during development i'm running the two instances on different ports. Trying anyway ...
One app is running on 127.0.0.1:8000, and the other on 127.0.0.1:9000.
I've tried running both on localhost or 0.0.0.0 and all other combinations but I keep getting these weird errors.
407 Client Error: Proxy Authorization Required
or
500 Server Error: INKApi Error
which as far as i could find is an apache error, or 403 forbidden
What is the correct way to test two apps on the same machine ?
There's no TRUE one. But you could try using nginx (althought this works with apache as well, using the appropiate syntax - what you should care about is the general idea). You could define two fake domains like domain1.dev and domain2.dev, and build appropiate entries:
server {
listen 80;
server_name domain1.dev;
location / {
proxy_pass http://127.0.0.1:8000/
}
}
server {
listen 80;
server_name domain2.dev;
location / {
proxy_pass http://127.0.0.1:9000/
}
}
note, however, this setup is incomplete and you should understand how nginx works. althought the idea here is conceptual and you can do this with, also, apache. ALSO the domains must be correctly defined in /etc/hosts (or the windows equivalent) for this to work.

Pylons 0.9.6 Get Current Server Name

In my Pylons config file, I have:
[server:main1]
port = 9090
...config here...
[server:main2]
port = 9091
...config here...
Which are ran using:
paster serve --server-name=main1 ...(more stuff)...
paster serve --server-name=main2 ...(more stuff)...
Now, using Haproxy and Stunnel, I have all http requests going to main1 and all https requests going to main2. I would like some of my controllers to react a little differently based on if they are being requested under http or https but pylons.request.scheme always thinks that it is under http even when it is not.
Seeing as I always know that main2 is always the one handling all https requests, is there a way for the controller to determine what sever name it was ran under or what id it is?
I got around this by just changing the workflow to not have to react differently based on what protocol it's under. It doesn't look like there's a way to pass a unique arbitrary identifier to each separate process that it can read.

Categories

Resources