Thursday, May 7, 2015

Re: Sending Data from client side(another computer) to Django(my computer) using Socket library

On Thu, May 7, 2015 at 9:24 AM, steve malise <stviaswggr@gmail.com> wrote:
>
> client side code:
> data = "message"
> try:
> clsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> clsocket.connect(('192.168.2.2', 8000))
> print("Connection has been Made")
> clsocket.send("POST / HTTP/1.1 "+ data)
> clsocket.close()
> except:
> print("ERROR:Connection is not established")

Eurgh. Your hand-crafted HTTP request is not a valid request. Use one
of the http libraries built in to python:

urllib2:
https://docs.python.org/2/howto/urllib2.html#data

httplib:
https://docs.python.org/2/library/httplib.html#examples

or use a 3rd party library that wraps those in a more pleasing interface:

requests:
http://docs.python-requests.org/en/latest/

>
> Django code(view.py)
>
> def RandomValues(request):
>
> template = get_template('TIME TABLE.html')
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> try:
> s.bind(('192.168.2.2',8000))
> s.listen(5)
> socketList.append(s)
> except:
> return HttpResponse("Unable to bind ")
>
>
>
> while 1:
> readyToread,readyTowrite,inError = select.select(socketList,[],[],1)
> for sock in readyToread:
>
> if sock == s:
> sockfd, addr = s.accept()
> socketList.append(sockfd)
> else:
> data = sock.recv(4096)
> message = data.decode("utf-8")
>
> return HttpResponse(message)

?

Why are you writing a webserver inside a view?

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1%2BS_w9zCk_Af5wMXZABFM%3D6k40nRWfp9P1gonsT5BNDFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment