R
R
rusyska550112021-06-09 00:47:29
Python
rusyska55011, 2021-06-09 00:47:29

How to decode string received from server?

def send_request(connection):
    data = connection.recv(1024).decode('utf-8')
    get_request = data.split('GET /')[1].split('HTTP/')[0].strip()
    try:
        price = int(get_request)
    except ValueError:
        content = b'Error'
    else:
        content = str(Algorithm(products_base, rating_base).build(price)).encode('utf-8')

    connection.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
    connection.send(content)


if __name__ == '__main__':
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('127.99.83.100', 2000))
    server.listen(1)
    while True:
        connection, address = server.accept()
        connection_thread = threading.Thread(target=send_request, args=(connection,))
        connection_thread.start()


This should come up:
(14270, [('null', 'Intel Core i3 Processor Haswell i3-4170 OEM', 5839, '/INTEL-I3-4170-OEM.htm', 'i3-4170 ', '1150', 54, 2, 4, '22', 3.7, 'null'), ('./cache/img/1805332.jpg', 'Cooling system ID-COOLING SE-913-B ', 948, '/ID-COOLING-SE-913 -B.htm', 'active cooler', 130, 1, 2200, 122, '1150, 1155/1156, 1151/1151 v2, 1200', 'am2/am3/fm1/fm2, am4'), ('. /cache/img/1204981.jpg', 'AFOX IH81-MA motherboard', 3650, '/AFOX-IH81-MA.htm', '1150', 'intel h81', '3', 1600, 2, ' micro-atx', 'hdmi', 'pci-e 16x - 1 pcs'), ('null', 'Hynix HMT DDR3 RAM 1x4Gb HMT451U6BFR8C-PBN0', 2041, '/HYNIX-HMT451U6BFR8C-PBN0.htm', '3', 4, 1600, '11-11-11-30'), ('./cache/img/173326.jpg', 'Gembird CCC CCC-PSU7', 203, '/GEMBIRD-CCC-PSU7.htm', 550, 'atx ', '24+4 pin, 1 x 6 pin', 'sata 2 pcs, molex (ide) 6 pcs'), ('./cache/img/140964.jpg', 'Hard drive Hitachi Deskstar 7K1000.C HDS721016CLA382 160\xa0GB', 1589, '/HITACHI-HDS721016CLA382.htm', 160, 7200, 'internal', '3.5')])3.5')])3.5')])

And this is output:
60bfe552bb22e534010063.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-06-09
@rusyska55011

Perhaps the browser just chooses the wrong encoding. You can simply manually select the page you need for this page through the menu to make sure of this. You can also add encoding to Content-Type:
Content-Type: text/html; charset=utf-8

D
Daler Hojimatov, 2021-06-09
@EtherDaler

Try it. Import the demjson module

import demjson
demjson.decode( ваш json )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question