how to use asyncore socket in django views.py

172 views
Skip to first unread message

Kay

unread,
Dec 22, 2011, 1:23:38 PM12/22/11
to Django users
if I have easy asyncore socket code as follow:

--------------------------------------------------------------------------------------------------------------

import socket
import asyncore

class asysocket(asyncore.dispatcher):

def __init__(self,host,port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(10)
print 'server is waiting for socket connection...'

def handle_accept(self):
new,addr = self.accept()
print 'received from address', addr
data = new.recv(1024)
print repr(data)

def handle_close(self):
self.close()

server = asysocket('127.0.0.1',1234)
asyncore.loop()

----------------------------------------------------------------------------------------------------------------

then how to use this in my django project views.py to let it
automatically work for receiving connection

while i start the project as "python manage.py runserver [port]"??

thanks

kay

Vovk Donets

unread,
Dec 27, 2011, 3:34:09 AM12/27/11
to django...@googlegroups.com
For what you need this?
Maybe you should consider using Celery for doing background jobs.

2011/12/23 Kay <fcapb...@gmail.com>

--
Vovk Donets
 python developer

skype:  suunbeeam
icq:      232490857
mail:    donets....@gmail.com
www:  jetfix.ru

Kay

unread,
Dec 27, 2011, 4:38:25 AM12/27/11
to Django users
Hello~

I need to run the python django project and meanwhile accept the
socket connection from somewhere to receive data.

But I dont know is this can be done,so any tips if this is possible?

thanks~

kay


On 12月27日, 下午4時34分, Vovk Donets <donets.vladi...@gmail.com> wrote:
> For what you need this?
> Maybe you should consider using Celery for doing background jobs.
>
> 2011/12/23 Kay <fcapba1...@gmail.com>
> *Vovk Donets*
>  python developer
>
> skype:  suunbeeam
> icq:      232490857
> mail:    donets.vladi...@gmail.com
> www:  jetfix.ru

Vovk Donets

unread,
Dec 27, 2011, 5:41:25 AM12/27/11
to django...@googlegroups.com
To accepts socket connetction you need always listen some port. Othewise you will be trying hit a bulls eye when sending data to this port (if you would listen port only when view was called) View is a function it's not a webserver that runs and listen to port.
You need to decouple working with sockets from view and place it somewere outside django

2011/12/27 Vovk Donets <donets....@gmail.com>

Joseph Slone

unread,
Dec 27, 2011, 2:23:17 PM12/27/11
to django...@googlegroups.com
Depending on your needs, Celery could be over-kill.  You could create a command that works under manage.py and use it in a cron job.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Joseph Slone
Reply all
Reply to author
Forward
0 new messages