gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

json data from server to client

Tue Jun 18, 2019 4:50 am

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

scotty101
Posts: 3597
Joined: Fri Jun 08, 2012 6:03 pm

Re: json data from server to client

Tue Jun 18, 2019 7:36 am

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
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

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.            
           
           
          

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

Tue Jun 18, 2019 10:26 am

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)


scotty101
Posts: 3597
Joined: Fri Jun 08, 2012 6:03 pm

Re: json data from server to client

Tue Jun 18, 2019 1:31 pm

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?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

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 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)
############################################
Last edited by gauravsharma0190 on Wed Jun 19, 2019 11:34 am, edited 1 time in total.

scotty101
Posts: 3597
Joined: Fri Jun 08, 2012 6:03 pm

Re: json data from server to client

Wed Jun 19, 2019 10:12 am

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..
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
B.Goode
Posts: 7857
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: json data from server to client

Wed Jun 19, 2019 10:38 am

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.

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

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.

User avatar
rpdom
Posts: 14417
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: json data from server to client

Thu Jun 20, 2019 7:11 am

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.

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

Fri Jun 21, 2019 5:51 am

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.

scotty101
Posts: 3597
Joined: Fri Jun 08, 2012 6:03 pm

Re: json data from server to client

Fri Jun 21, 2019 7:43 am

How about you try that and find out whether it works or not?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

gauravsharma0190
Posts: 112
Joined: Tue Oct 28, 2014 6:36 am

Re: json data from server to client

Mon Jun 24, 2019 4:55 am

yes it works
Thank you

Return to “Networking and servers”