-1

I'm trying to send a JSON File from a server to a client. The problem is, in the JSON File there is a special char 'Ć' and when i recieve the file in the client it's written 'F'. I'm not sure if i have to decode/encode it.

Here is the server:

import socket                   

sourcefile = 'TMS.JSON'
TCP_IP = '10.0.9.6'    #IP of server!
TCP_PORT = 3487

s = socket.socket()
s.bind((TCP_IP, TCP_PORT))
s.listen(5)

print('DEBUG: Server listening....')
while True:
    conn, addr = s.accept()
    print('DEGUG: Got connection from', addr)

    f = open(sourcefile, 'rb')
    l = f.read(1024)
    while (l):
       conn.send(l)
       l = f.read(1024)
    f.close()
    print('DEGUG: Done sending')
    conn.close()

Here is the client:

import socket
import json

destinationfile = 'TMS_recieved.JSON'
TCP_IP = '10.0.9.6'    #IP of server!
TCP_PORT = 3487

s = socket.socket()
s.connect((TCP_IP, TCP_PORT))

print('DEBUG: Recieving data....')
with open(destinationfile, 'wb') as f:      #create JSON FILE
    while True:
        data = s.recv(1024)
        if not data:
            break
        f.write(data)
f.close()

print('DEBUG: Successfully get the file')
s.close()
print('DEBUG: Connection closed')

with open(destinationfile, 'r') as f:       #read JSON FILE
    datastore = json.load(f)


print()
print('datastore['name'])

Can please someone help me? Thanks

Sheri
  • 1,383
  • 3
  • 10
  • 26
stribudaS
  • 7
  • 2
  • 3
    On the server side, use `.encode('utf-8')` when sending the data, and on the client side use `.decode('utf-8')` when receiving the data – Giorgos Myrianthous Nov 21 '19 at 17:10
  • But where should i use encode and decode? – stribudaS Nov 21 '19 at 17:16
  • 3
    @GiorgosMyrianthous since the files are opened with 'b' binary mode, there should be no encoding/decoding necessary if they're already in UTF-8. – Mark Ransom Nov 21 '19 at 17:43
  • `f.close()` after `with open(...) as f` is redundant. Other that that, I'm wondering what the problem actually is, kind-of like a [mcve] and a precise description of the observations, not just your paraphrased interpretation. – Ulrich Eckhardt Nov 21 '19 at 19:49
  • @Ruan well i tried that and i'm getting this error: `print('datastore['name']) UnicodeEncodeError: 'ascii' codec can't encode character '\u0106' in position 28: ordinal not in range(128)` – stribudaS Nov 27 '19 at 13:52
  • Does this answer your question? [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – Ulrich Eckhardt Dec 08 '19 at 12:45

1 Answers1

0

At client.py replace

with open(destinationfile, 'r') as f:
    datastore = json.load(f)

with

with open(destinationfile, 'rb') as f:
    datastore = f.read().decode('UTF-8')

datastore = json.loads(datastore)

And then it works for me if TMS.JSON is

{"name": "Ć"}

Output:

DEBUG: Recieving data....
DEBUG: Successfully get the file
DEBUG: Connection closed

Ć
Ruan
  • 219
  • 5
  • 9