I want to get data by websocket using asyncio with django custom command.
The reason I use django is because I use ORM and I want to use the data obtained from websocket as a web application.
I tried to make it by inserting a loop into the hundred method from the explanation of HP, but I could not receive any data.
I also added the following statement to setting.py.
os.environ ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
If anyone knows the cause, please give us your opinion.
import asyncio import json import websockets class Command (BaseCommand): def handle (self, * args, ** kwargs): "" "Start loop" "" loop = asyncio.get_event_loop () loop.create_task (self.stream ()) try: try: loop.run_forever () except KeyboardInterrupt: exit () async def stream (self): uri ='ws: // localhost: 18081/****/websocket' async with websockets.connect (uri, ping_timeout = None) as ws: while not ws.closed: response = await ws.recv () content = json.loads (response) score = self.original_method1 (kwargs) print ('reveiving') self.original_method2 (kwargs)
Answer # 1
It doesn't seem to have to be a Django custom command.
You can call and use the model created by Django from any program.
As a reference, I think this page will be helpful.
The following is an example.
import django sys.path.append ("/ var/www/app") Where # manage.py is os.environ.setdefault ('DJANGO_SETTINGS_MODULE','app.settings') # settings.py django.setup () from appname import models # model import
- python - i want to change the ip address that the django app gets by default from remote_addr to http_x_forwarded_for in middlew
- python - [django] gte works with filter in queryset, but lt doesn't work and i can't get the specified range of data
- python - django i want to filter the data associated with a foreign key and display it in a template
- python - django image folder reasons to create static and madia folders separately
- python - about image deletion in django
- python - object is not displayed in html in django queryset
- name error in underscore in python django
- python - how to count url clicks in django and record them in the db
- python - all commands cannot be used with discord bot
- python - i can't find some url paths after deploying django
- python - i can't display django images
- python - django form i want to write a little smarter
- python - send commands from windows bat to running processes
- python - when i set the uuid field in django, i get field'object_id' expected a number but got '78a0767cb607488087a73f17565bb
- python - when will you start the startapp in django?
- python - the table element generated by the django template variable is output at the bottom of the page
- python - [django] read csv file from url
- python - i get an argument must be int or float error in django
- python - django how to receive multiple checkbox values
- python : Django Render does not open HTML Page
- How to use the same model object in different templates. Python. Django.
- Ready sources of small projects and programs on Python
- python : How to display objects associated via ForeignKey in Django pattern?
- python : Models and forms in Django
- python : How to make username from other fields?
- python : PycharmPro Installation Error
- python : Download Django Server File
- python : Model with quantity in Django ORM
- python : Django Forms auto-generated field