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

390 views
Skip to first unread message

steve malise

unread,
May 7, 2015, 6:03:32 AM5/7/15
to django...@googlegroups.com

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")

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)

On my command prompt it says:
[07/May/2015 10:06:21]code 400, message Bad request syntax ('POST / HTTP/1.1 message')
[07/May/2015 10:06:21]"POST / HTTP/1.1 message" 400 -

Even if i remove "POST / HTTP/1.1",i still get this error

please help i am new to Django



Tom Evans

unread,
May 7, 2015, 6:42:34 AM5/7/15
to django...@googlegroups.com
On Thu, May 7, 2015 at 9:24 AM, steve malise <stvia...@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

steve malise

unread,
May 7, 2015, 7:48:53 AM5/7/15
to django...@googlegroups.com
where can i write web server?



--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Tom Evans

unread,
May 7, 2015, 9:28:57 AM5/7/15
to django...@googlegroups.com
On Thu, May 7, 2015 at 12:48 PM, steve malise <stvia...@gmail.com> wrote:
> where can i write web server?
>

Why do you want to?

Django is a web application, it is hosted inside webservers, typically
using WSGI. Typically, you would use one of the many webservers that
can host wsgi applications - nginx, apache+mod_wsgi, uwsgi, chaussette
and many many others. You can also use the built-in "runserver"
webserver for development.

If you want to write your own webserver for your own edification, the
wsgiref library is built in to python to provide a reference
implementation.

Cheers

Tom

steve malise

unread,
May 7, 2015, 10:00:45 AM5/7/15
to django...@googlegroups.com
What i am trying to do is run python script(client side) on another computer which is on same network with my computer,then receive data from another computer with the django(which is running on my computer(server side)).i am using built-in "runserver".

The error comes when i runserver and send data from another computer to django,then i get this "code 400, message Bad request syntax ( data from another computer)"




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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Tom Evans

unread,
May 7, 2015, 10:06:20 AM5/7/15
to django...@googlegroups.com
On Thu, May 7, 2015 at 3:00 PM, steve malise <stvia...@gmail.com> wrote:
> What i am trying to do is run python script(client side) on another computer
> which is on same network with my computer,then receive data from another
> computer with the django(which is running on my computer(server side)).i am
> using built-in "runserver".
>
> The error comes when i runserver and send data from another computer to
> django,then i get this "code 400, message Bad request syntax ( data from
> another computer)"
>

Write a simple django view like this:

def myview(request):
return HttpResponse(request.raw_post_data)

In your client python script, use one of the libraries I mentioned, eg requests:

import requests
req = requests.post('http://hostname/url', data='hello world')
print req.text

Go from there..

Cheers

Tom

Thomas Levine

unread,
May 7, 2015, 10:12:31 AM5/7/15
to django...@googlegroups.com
On 07 May 16:00, steve malise wrote:
> The error comes when i runserver and send data from another computer to
> django,then i get this "code 400, message Bad request syntax ( data from
> another computer)"

This is because

On 07 May 11:42, Tom Evans wrote:
> Eurgh. Your hand-crafted HTTP request is not a valid request. Use one
> of the http libraries built in to python:

You can find here some directions on forming the request validly,
http://www.w3.org/Protocols/Specs.html

as you apparently don't want to use any of the libraries below.

On 07 May 16:00, steve malise wrote:
> 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/

Using one of the above-listed libraries would still accomplish what you
are trying to do, as you described here,

On 07 May 16:00, steve malise wrote:
> What i am trying to do is run python script(client side) on another
> computer which is on same network with my computer,then receive data from
> another computer with the django(which is running on my computer(server
> side)).i am using built-in "runserver".

so I suspect that you are doing something more that we missed. Perhaps
you could tell us why you want to do this?

steve malise

unread,
May 11, 2015, 9:09:59 AM5/11/15
to django...@googlegroups.com
client code:
import httplib
import urllib

data = urllib.urlencode({'data':'data1'})
conn = httplib.HTTPConnection(myipaddr)
headers = {"Content-type":"application/x-www-form-urlencoded",
                "Accept":"text/plain",
                 "Content-Length:":len(data)}
h.request('POST','/',data,headers)

res = h.getresponse()
print r.status,r.reason

server side code(view.py):

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.conf.urls import url
from django.template import loader
from django.utils.timezone import utc
from django.template import Context, Template
from django.template.loader import get_template
from django.shortcuts import render_to_response
from django.template import RequestContext
from myapp.models import Temperature
from myapp import models
import socket , select
import os
import datetime
socktetList=[]


@csrf_exempt
def myview(request):
    if request.method == 'POST':
        return HttpResponse("%s %s" % (request.method, request.body)) #print POST body
    else:
        return HttpResponse("%s %s" % (request.method, request.body))

on the client side i get "200 OK"(everything is ok)
but when i open my browser it return "GET",

i want it to return or display POST DATA.

how can i do that??
please help



--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Guilherme Leal

unread,
May 11, 2015, 9:14:07 AM5/11/15
to django...@googlegroups.com
The "navigation" of your browser always performs a GET method. If you're trying to test that kind of request in the browser, the best aproach would be open the JavaScript and make the request there.

steve malise

unread,
May 11, 2015, 9:18:56 AM5/11/15
to django...@googlegroups.com
Meaning write JavaScript code to make a request?
how can achieve it other than writing JavaScript code?


Guilherme Leal

unread,
May 11, 2015, 9:26:53 AM5/11/15
to django...@googlegroups.com
Just to clarify, in your browser, if you simply "navigate" to the url of your view, the browser will issue a GET to the server.

If you write the JavaScript function, you can specify the method of your request... preaty much the same thing as you did in the exemple that you sent.

Its fairly trivial, and JQuery makes it even simpler.



Jani Tiainen

unread,
May 11, 2015, 9:32:41 AM5/11/15
to django...@googlegroups.com
Hi,

Sounds like you're mixing up terms and technologies here and really you are trying something that Django was never designed for.

It would be really helpful to know what are you really trying to do, instead of just trying techniques that may or may not work for your case.
> >> <https://groups.google.com/d/msgid/django-users/CACwh%3D0zjAYKjmyTJTWcucTyLM_iVaLokcgqREZpXgzp7e_GEGw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> >> .
> >> For more options, visit https://groups.google.com/d/optout.
> >>
> >
> > --
> > 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...@googlegroups.com.
> > To post to this group, send email to django...@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/CAOs3Lp5N5RaBE7VRazeeA3gfc0Ccc5pRO7MNzCe-Z_rTr1Nsnw%40mail.gmail.com
> > <https://groups.google.com/d/msgid/django-users/CAOs3Lp5N5RaBE7VRazeeA3gfc0Ccc5pRO7MNzCe-Z_rTr1Nsnw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> > .
> >
> > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/CACwh%3D0zKZrqHovzc%3DY0MAaBp21k9YvbAHqCBFdwH9knx8vr9LA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

steve malise

unread,
May 11, 2015, 9:51:30 AM5/11/15
to django...@googlegroups.com
I want to display HTTP POST data from client side to on the server side and
what am supposed do to achieve?

Guilherme Leal

unread,
May 11, 2015, 10:12:09 AM5/11/15
to django...@googlegroups.com
Puting plain, you just need to execute a POST request on the client side. 
That makes aviable the content on the request.POST property on your client side view.

Wich tecnology you use to make the post request doesnt matter. The only thing you need to keep in mind is when you "navigate" through your browser, you're making a GET request, not a POST. Thats why your request didnt had a body on the exemple you previously provided.

If you explained a little bit more of your app context, would be easier to explain more.

Reply all
Reply to author
Forward
0 new messages