Django wsgi server bjoern multithreaded - django

i would like to use bjoern wsgi server, currently this is working fine but i want to get it multithreaded so that i can access my application localy on multiple ports not only on port 8080 but also und 8081, 8082 etc...
currently i do the following at my run.py file which starts the django app:
import bjoern
from app.wsgi import application
bjoern.run(application, 'localhost', 8080, reuse_port=True)
how can i spawn multiple processes of run.py on diffrent ports?
Or in general how can i use multithreading at this point?
The documentation seems quite complicated to me or at least i dont know where to start, https://docs.python.org/3/library/threading.html.
Thanks in advance

Related

How to replace port 8000

I'm working on a project now and I'm currently using Django+uWSGI+Nginx to deploy the backend on the server. There is also a frontend using Vue.js on the same server.
So the frontend is www.mysite.com
The backend uses port 8000 as www.mysite.com:8000
But I encountered a problem, that is, many users' work network blocked port 8000, so that users could only use the front end, but could not connect to the back end.
Is there any way to avoid using www.mysite.com:8000 and replace it with another url?
when you run the server you can specify the port:
python3 manage.py runserver 8000
you can also modify manage.py:
from django.core.management.commands.runserver import Command as runserver
runserver.default_port = "8000"
(In your case replace 8000 with the port you want)

Django Channels App Websocket Connection Failing

I believe I have a similar issue to the one found here
and here.
The gist is that I'm running a Django app with channels on an Amazon-ec2 instance and the websockets are failing to connect.
Most of my code regarding the websockets comes from the django-channels tutorial here.
Traffic is being directed though secured dns name behind an application load balancer.
I'm running this entirely on Daphne (handling both https and websocket traffic) with pretty minimal configurations:
daphne -b <server_url> -p <port> test_app.asgi:application
I'm also authenticating with openID-connect using the mozilla-django-oidc module. However for the websocket test I'm not expecting authentication. I feel it's worth pointing out if the issue is related to websocket authentication in any way.
In development I'm running a local redis cluster as my channel layer. My dev app (all http:// and ws://) has no issues connecting to websockets. The chat app works as expected. I can connect to my websockets and is confirmed with a
127.0.0.1:60779 - - [07/Apr/2021:12:06:05] "WSCONNECTING /ws/chat/lobby/"
Here is the code in asgi.py
import chat.routing
asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
application = ProtocolTypeRouter(
{
"http": asgi_app,
"websocket": AuthMiddlewareStack(
URLRouter(chat.routing.websocket_urlpatterns)
),
}
)
and the code in chat/routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),
]
In production I'm using an elasticache redis cluster as my channel layer. I can test this in the django shell and it connects/sends/receives.
However in the production chat I am unable to reach the room or see the above WSCONNECTING message. It never upgrades the connection to a websocket.
The next log after that is
2021-04-07 16:15:15,279 WARNING Not Found: /ws/chat/lobby/
Like its trying to resolve that route as http and not as a websocket.
After doing some additional reading I tried to use the host/port that is running on Daphne like
wss://<my host ip>:<port>/ws/chat/lobby
Which did not cause an immediate failure but ultimately never connected.
(index):46 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
Here's the error in chrome
I feel like it's something to do with that public-facing host name that is being used as the host name in the websocket url. All inbound traffic is allowed. I'm not sure how this relates to the issue at hand.
I'm still trying to learn my way around all this. Any advice would certainly be appreciated.
Thanks!
I was just running thru that same tutorial, and I got the same error. I decided to look at the methods imported here:
from channels.routing import ProtocolTypeRouter, URLRouter
and I couldn't find them. So I uninstalled django-channels and installed channels
python3 -m pip uninstall django-channels && python3 -m pip install channels
and stuff worked.

How to serve Flask app on different port than main site running on port 80?

Read through the other threads, but none seemed to address my need.
I have a website running fine on default port 80/443 using Apache on CentOS 7.
I have now installed gunicorn and copied my files to a /api subdirectory on the server. I am able to start gunicorn successfully using "gunicorn -b 0.0.0.0:8000 api:app"; however, I don't seem to be able to browse to https://example.com:8000 (site can't be reached).
Do I need to use a VirtualHost's file specifically for the Flask /api directory? If so, what would that look like without disrupting the main site?
EDIT: Note this is a Flask-RESTful API, no static files or anything on the Flask side. Just endpoints/routes to serve.

Gunicorn does't serve the flask app

I have an issue to serve my flask app with gunicorn on ubuntu 16 (google computer engine instance).
I used the application factory pattern to create the app so, from my virtualenv, I've started gunicorn with the following instruction
gunicorn -w 3 "app:create_app()" --bind 0.0.0.0:8080
It seems that guniconr run correctly but I'm not able to reach the website.
I create the istance allowing HTTP trafic but I don't know if I have to define a specific firewall rule or if the problem is somewhere else.
Thanks in advance for your help.

mac: simplest way to safely serve a django app on port 80 for development

I'd like to open up my django app to other machines in the office during development.
I understand that it's a bad idea to run the django development server as root. The recommended way to serve a django app on port 80, even during development, appears to be django, plus gunicorn, plus nginx. This seems super complicated to me. I got the first two steps working, but am now staring at nginx in utter confusion. There's no mac build on the site. Do I really have to build it from the source?
One alternative I've come across is localtunnel. But this seems sketchy to me, and involves setting up public keys and whatnot. Is there any simpler way to serve a django app on a mac from port 80 without running it as root?
Also, just what are the risks of running a django development server on port 80 as root, vs not as root? What are the chances that someone could, say, gain total access to my file system? And, given the default user settings on a mac, is this more likely if I'm running my django dev server as root than if I'm running it as not-root?
Since you mentioned you don't want to run the Django server as root and you are on a mac, you could forward traffic from port 80 to port 8000:
sudo ipfw add 100 fwd 127.0.0.1,8000 tcp from any to any 80 in
and then run the Django server as a normal user (by default it serves on port 8000)
./manage.py runserver
To remove the port forwarding, run:
sudo ipfw flush