Page 1 of 1

json data from server to client

Posted: Tue Jun 18, 2019 4:50 am
by gauravsharma0190
hello there,
i am working in client server model where i want to send some json data from server to the client. using socket programming and by reading the json format like json.dump and json.dumps() how can i do it.
please help.
i have seen some examples but this only shows the saving of json data in a file

Re: json data from server to client

Posted: Tue Jun 18, 2019 7:36 am
by scotty101
Python has lots of wonderful libraries to help with things like this (although the core library also contains ways to do this).

You can setup an HTTP server in python using Flask and on the other end you can use Requests to access the server and get the JSON data.
http://flask.pocoo.org/
https://2.python-requests.org/en/master/
https://2.python-requests.org/en/master ... se-content

This could all of course be done with a lower level TCP server/client model
https://wiki.python.org/moin/TcpCommunication

Re: json data from server to client

Posted: Tue Jun 18, 2019 9:04 am
by gauravsharma0190
hello scooty
i read the flask library . i just read the http and REST API from which i can use to send and receive the json data using the get and post request.
Here i write a code in python

Code: Select all

import json, requests
data = {
             'user' : 'gaurav'
             'var' : '7892'
             }
payload = json.dumps(data)
url = ' '
r = request.post(url , payload)

this is the one code .here i don't understand what url should i write for my localhost because i want to post this to the another code in which i can get the json data and save this in a file.            
           
           
          

Re: json data from server to client

Posted: Tue Jun 18, 2019 10:26 am
by gauravsharma0190
i am installing the Flask onto my Pi board and follows the linkhttp://flask.pocoo.org/docs/1.0/install ... stallation
now i activated the venv service and then pip install Flask
but there is error i don't understand what it this

Code: Select all

Exception:
Traceback (most recent call last):
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 350, in _make_request
    self._validate_conn(conn)
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 837, in _validate_conn
    conn.connect()
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connection.py", line 323, in connect
    ssl_context=context)
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/ssl_.py", line 324, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3.5/ssl.py", line 385, in wrap_socket
    _context=self)
  File "/usr/lib/python3.5/ssl.py", line 760, in __init__
    self.do_handshake()
  File "/usr/lib/python3.5/ssl.py", line 996, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.5/ssl.py", line 641, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/myproject/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/pi/myproject/venv/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 624, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/download.py", line 853, in _download_http_url
    stream=True,
  File "/home/pi/myproject/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/home/pi/myproject/venv/lib/python3.5/site-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/home/pi/myproject/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/pi/myproject/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/pi/myproject/venv/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/home/pi/myproject/venv/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 497, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)


Re: json data from server to client

Posted: Tue Jun 18, 2019 1:31 pm
by scotty101
gauravsharma0190 wrote:
Tue Jun 18, 2019 9:04 am
hello scooty
i read the flask library . i just read the http and REST API from which i can use to send and receive the json data using the get and post request.
Here i write a code in python

Code: Select all

import json, requests
data = {
             'user' : 'gaurav'
             'var' : '7892'
             }
payload = json.dumps(data)
url = ' '
r = request.post(url , payload)
this is the one code .here i don't understand what url should i write for my localhost because i want to post this to the another code in which i can get the json data and save this in a file.
The url should be either the IP address of the device you are trying to connect to or its actual url.
For localhost, the url could either be 'localhost:<portnum>' or '127.0.0.1:<portnum>'
where <portnum> is the port expecting to receive the HTTP request, normally 80, but with flask maybe something different like 8080.

No idea about your installation problem. I never use venv. How are you connected to the internet? Is it though a corporate firewall?

Re: json data from server to client

Posted: Wed Jun 19, 2019 9:53 am
by gauravsharma0190
Thank you sir
I need a small help from you.
Actually i have two code of server and client in TCP/UDP communication , and i am communicating through it mean message to the client and server.
Now what i want , just to send the json data to the client from the server and from server to the client.
How can i do that as i found a link https://github.com/mdebbar/jsonsocket/t ... d1447ece2e
i save jsonsocket.py and server and client.py .
but when i run these code with the python client.py
and python server.py but there is an error in this code.

############################################
[email protected]:~ $ python2 server2.py
Traceback (most recent call last):
File "server2.py", line 5, in <module>
server = Server(host, port)
File "/home/pi/jsonsocket.py", line 20, in __init__
self.socket.bind((host, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
############################################

Re: json data from server to client

Posted: Wed Jun 19, 2019 10:12 am
by scotty101
gauravsharma0190 wrote:
Wed Jun 19, 2019 9:53 am
Actually i have two code of server and client in TCP/UDP communication , and i am communicating through it mean message to the client and server.
If you already have code for TCP communication, why can't you just send the JSON data in the same way?

I have no idea what "it mean message" is..

Re: json data from server to client

Posted: Wed Jun 19, 2019 10:38 am
by B.Goode
gauravsharma0190 wrote:
Wed Jun 19, 2019 9:53 am
Thank you sir
I need a small help from you.
Actually i have two code of server and client in TCP/UDP communication , and i am communicating through it mean message to the client and server.
Now what i want , just to send the json data to the client from the server and from server to the client.
How can i do that as i didn't find anything on net.


Quoting from Wikipedia:
In computing, JavaScript Object Notation (JSON) (/ˈdʒeɪsən/ "Jason", /ˈdʒeɪsɒn/) is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format used for asynchronous browser–server communication, including as a replacement for XML in some AJAX-style systems.
Ref: https://en.m.wikipedia.org/wiki/JSON


Note - " an open-standard file format that uses human-readable text "


So if you already have code that can send messages between client and server, all you have to do is use the JSON-encoded data as the message string, or 'payload'. You don't need any more specialised communication protocol just because the message happens to be using JSON encoding.

Re: json data from server to client

Posted: Thu Jun 20, 2019 4:37 am
by gauravsharma0190
You mean to say instead of sending a string from client using the .send() function i send the json data to the server and on the server side receive it via recv() function.

Re: json data from server to client

Posted: Thu Jun 20, 2019 7:11 am
by rpdom
gauravsharma0190 wrote:
Thu Jun 20, 2019 4:37 am
You mean to say instead of sending a string from client using the .send() function i send the json data to the server and on the server side receive it via recv() function.
Yes. JSON data is a string. That is the main point of it.

Re: json data from server to client

Posted: Fri Jun 21, 2019 5:51 am
by gauravsharma0190
hello
thank you for your reply.
for eg:
i created a json data DATA ={
"vehicle_id" : "78a4ff53'
}
if i want to send it to the client side using the s.sendall(DATA.encode('utf-8') . then should i send it using this
please tell me.

Re: json data from server to client

Posted: Fri Jun 21, 2019 7:43 am
by scotty101
How about you try that and find out whether it works or not?

Re: json data from server to client

Posted: Mon Jun 24, 2019 4:55 am
by gauravsharma0190
yes it works
Thank you