Django + tcp/ip socket

Hi guys,
I´m new with django and python and i have a question for you.
In my project i need a tcp/ip socket to listen for one client who is sending data. The client is already done and i cant change it.
My idea is to create a new thread and inside that tread open the socket but where can i instantiate this thread and socket? i need it in the runserver. Start all things at the same time.

Thanks for the help

Nope, you really, really, really don’t want to do this in runserver.
Write a separate management command and run it as a separate process.
(Of course, if you’re running your production Django environment using runserver, you’ve already got a problem. From the runserver docs:

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

3 Likes

I´m new, so i´m using runserver because i followed the introduction tutorial.
So you are saying to create a separete server for the socket and logic right? but i cant acess django ORM in taht way right?

Sorry for the nobbie questions

When you’re ready to actually deploy your project into a production environment, you’ll want to read the docs on Deploying Django and the pages referenced by it.

That is not a correct statement. One of the advantages of writing a django-admin command is that you have full access to the ORM. It’s executed using the same settings file as your regular Django project, so you have exactly the same access to the Models and Data as your project.

Keep in mind that “runserver” is a django-admin command that starts a socket listener. So what you’d be writing is logically just another type of runserver command - only instead of listening for http requests, it accepts requests from the custom client.

1 Like

Wow i see. Something like add another app to the project for example “python manage.py startapp tcpserver” create a new type of “runserver command” to that app will do the trick? or its not possible? or silly?

I wouldn’t create another app for that, I would just create the command within the management/commands directory in your primary app.

That’s exactly what we do with our data collection apps. It’s run as manage.py listener <port> where listener.py is a file in the “/base/management/commands/” directory, and <port> is the numerical port that that instance listens to.

1 Like

Wow, i will try it for sure, right now. Thanks for the wisdom!

And it works like a charm! thank you very mutch! One last question, my socket will consume one thread in a while true loop, its better create a new thread for the socket? or django will manage that? And i can acess Django channels right?

Bit of a terminology issue here - whenever you’re using Django, “Django” is not managing any socket connections itself - ever. Your wsgi container (e.g. uwsgi or gunicorn) is what’s managing your sockets. Since you’re running a separate process from them, they wouldn’t be involved in your connections.

If you’re needing to handle multiple concurrent TCP socket connections, then you can either use a threaded handler or an async socket manager. Some searching around should yield any number of examples you can draw from. But I’d start from the official Python library docs for socketserver.

If by “access Django channels” you’re referring to something like accessing the channels layer from outside a consumer, then yes, this should permit that.

2 Likes