在socket编程python中发送json格式的字节到服务器

oo7oh9g9  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(154)

我写了一个套接字
我想做的是选择一个文件
之后,阅读文件
然后将读取的文件转换为一个列表并建立一个字节列表
并且每次我将这个列表的其中一个索引以JSON格式单独发送到服务器
在服务器端加载JSON并打印
一切都好吗
这意味着文件被正确读取
正确转换为字节列表
并以JSON格式正确发送到服务器
但是在服务器端,当我想把它从JSON格式中取出来的时候
我得到这个错误->

{'type': 'fileUpload', 'file': 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAA7EAAAOxAGVKw4bAAATPElEQVR42s1be2xT1R//3Hv7Wtm6dXNjZWxswgabDsabgThIjAMVBKe44BOjRjBEUYnySAQfBBPBVwJBxWCMiqIoBAQHSoLKBBcCDDYeY2wdm92rXdv1dV/f3x+kl7a7bccYv/hNbnp7envu+Z7zPd/v53zO9zB0TQAADMMgeB/8DgChvwe/q92HSrTy0N8i6+np6QHDMOjo6IAkSRg9ejRYlgURQRAE/PPPPxg5ciQyMzP7VX+0d4XpFNS+u7sbAJCWlgYigtfrxZAhQ/r8IbSj1CqO1qDITlVrsCzLAABZlkFE0Gq1yjOSJKGzsxPJyckwGAzqykRRNFZ7NcGHAoEAJElCWlqa8kI15dQqH4hIkgRJkiDLMliWhU6nA8uyUS2N4zhl5COVjmxD6P/UPsN+p9DhiaJYNJONvJckCe3t7ZBlGSaTCQaDARzHQZZldHd3w+12w2q1orq6Gr29vfB4PHC73TCZTHj44YcxdepUsCwLnueRkJCgmP/NTLdY7QcARpZl6q9Jhz6jZg29vb147rnncPjwYZjNZlgsFuTk5MDhcODkyZNwOp0IBAKKqYeKwWDAQw89hKFDh6KmpgZlZWV48sknMWrUqD7vVBmzfvuryGmsiTSRWBKtg2RZhtVqRV1dHXieR3d3N7q7u9HY2IiamhqwLIsFCxZAlmV89913qnX7/X58++23yqg7HA40NTVh+PDhKC8vx6hRo5Camgqj0RjVCgKBAHQ6HQRBgF6vj9pJoR2jiWc28aZGU1MTvvrqK9TV1cFsNkMQBBgMBoiiiFmzZiE/Px88zyMjIwP//POP6ujpdDoYDAbk5eXh6tWrcLlcEAQBL774Ik6ePInXX38dDMNg69atKCkpUVWEYRhoNBowDAOO42JGqzA/Qv0QWZb7lEmSRDU1NTR79myaMmUKVVVV0Z49e+iJJ56glJQUAkAcxxHHcQQg7GIYRrlnWZays7NJr9fTkiVLqKSkhMaMGUNFRUW0cuVKmjZtGqWkpBDDMPTII4/QqVOniOf5G2qzWvuDguCPkX8IXoFAgCKfkWWZqquracGCBfTll1+S1+ul48eP04QJEwgAaTSaPkoDoJycHCorK6MHHniAWJYlAGSxWOi+++6j4uJiWrRoERUWFtI777xDSUlJlJ2dTUOHDqXExESljvT0dNqxY4dqeyPv1Tol8jlWzQeEmorH41Gdr7t37wbP87jnnntw9OhRrFq1CmfPngUAiKKoGqK0Wi1mzJiBuXPnwmQyISsrC2VlZejq6sKiRYtQWVmJlA=='}
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\threading.py", line 980, in _bootstrap_inner
    self.run()
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\PC\Desktop\New folder (5)\server.py", line 11, in listen_for_messages
    message = json.loads(message)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 1403 (char 1402)

字符串
client.py

from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import socket
import threading
import json
import os
import base64

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.show()

        self.fileBtn = QPushButton('file upload')
        self.fileBtn.clicked.connect(self.fileUpload)
        mainWidget = QWidget()
        layout = QVBoxLayout()
        mainWidget.setLayout(layout)
        layout.addWidget(self.fileBtn)
        self.setCentralWidget(mainWidget)

        self.connect()

    def connect(self):
        self.HOST = '127.0.0.1'
        self.PORT = 1234
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client.connect((self.HOST, self.PORT))

    def fileUpload(self):
        fileName, ok = QFileDialog.getOpenFileName(self)
        if ok:
            file = open(fileName , 'rb')
            file = file.read()
            _range = 1024
            def reader(data):
                for i in range(0, len(data), _range):
                    yield data[i:i+_range]
            lst = list(reader(file))

            for elem in lst:
                dic = json.dumps({
                    "type":"fileUpload",
                    "file":base64.b64encode(elem).decode('utf-8'),
                })
                self.client.sendall(bytes(dic,encoding="utf-8"))      

app = QApplication([])
win = Window()
app.exec()


server.py

import socket
import threading
import json
LISTENER_LIMIT = 5
HOST = '127.0.0.1'
PORT = 1234

def listen_for_messages(client):
    while True:
        message = client.recv(2048).decode('utf-8')
        message = json.loads(message)
        print(message)

def main():

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        server.bind((HOST, PORT))
    except:
        print(f"Unable to bind to host {HOST} and port {PORT}")
    server.listen(LISTENER_LIMIT)
    while 1:
        client,address = server.accept()
        threading.Thread(target=listen_for_messages, args=(client, )).start()

if __name__ == '__main__':
    main()

wqlqzqxt

wqlqzqxt1#

TCP是一种流协议。没有消息边界。您可以自行读取字节流并对其进行解析以获取消息。
一个简单的方法是使“message”成为以换行符终止的字符串。socket有一个.makefile方法,该方法将TCP流 Package 在一个类似文件的对象中。此对象具有.readline方法。将每个JSON作为以换行符终止的行发送。
范例:

服务器.py

import socket
import threading
import json

def listen_for_messages(client, address):
    with client.makefile('rb') as rfile:
        while True:
            line = rfile.readline()
            if not line: break
            message = json.loads(line)
            print(f'{address}: {message}')

server = socket.socket()
server.bind(('', 1234))
server.listen()
while True:
    client, address = server.accept()
    threading.Thread(target=listen_for_messages, args=(client, address)).start()

字符串

客户端.py

import socket
import json

with socket.socket() as s:
    s.connect(('localhost', 1234))
    lst = ['abc', 'def', 'ghi']
    for elem in lst:
        dic = json.dumps({'type': 'fileUpload', 'file': elem}) + '\n'
        s.sendall(dic.encode())


启动client.py并运行几次。server.py
输出示例:

('127.0.0.1', 21062): {'type': 'fileUpload', 'file': 'abc'}
('127.0.0.1', 21062): {'type': 'fileUpload', 'file': 'def'}
('127.0.0.1', 21062): {'type': 'fileUpload', 'file': 'ghi'}
('127.0.0.1', 21065): {'type': 'fileUpload', 'file': 'abc'}
('127.0.0.1', 21065): {'type': 'fileUpload', 'file': 'def'}
('127.0.0.1', 21065): {'type': 'fileUpload', 'file': 'ghi'}
('127.0.0.1', 21278): {'type': 'fileUpload', 'file': 'abc'}
('127.0.0.1', 21278): {'type': 'fileUpload', 'file': 'def'}
('127.0.0.1', 21278): {'type': 'fileUpload', 'file': 'ghi'}

相关问题