So, I created client and server sing socket.
And everything works well, but i just broke then using string from MySQL database.
Server:
import socket
import threading
PORT = 9696
SERVER = "localhost"
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn):
while True:
actual_size = int(conn.recv(30).decode(FORMAT).replace("#", ""))
size = int(actual_size / 1024)
rest_msg = actual_size - (size * 1024)
received_message = ""
for chunk in range(size):
received_message = conn.recv(1024).decode(FORMAT)
received_message = conn.recv(rest_msg).decode(FORMAT)
print(rest_msg)
print(len(received_message))
def start():
server.listen()
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn,))
thread.start()
start()
Client:
import socket
import time
HEADER = 64
PORT = 9696
FORMAT = 'utf-8'
SERVER = "localhost"
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
client.send(msg.encode("utf-8"))
msg_to_send = "Some random message"
time.sleep(3)
for i in range(1000):
send(str(len(msg_to_send)) ("#" * (30 - len(str(len(msg_to_send)))) msg_to_send))
while True:
pass
If I replace msg_to_send
with following text:
[('index', 'bigint(20)', 'YES', 'MUL', None, ''), ('LP', 'text', 'YES', '', None, ''), (' NR-a', 'text', 'YES', '', None, ''), ('LITERA-a', 'text', 'YES', '', None, ''), ('MUFA-a', 'double', 'YES', '', None, ''), ('UWAGI-a', 'text', 'YES', '', None, ''), ('TUBY-a', 'double', 'YES', '', None, ''), ('DATA1-a', 'text', 'YES', '', None, ''), ('DATA2-a', 'text', 'YES', '', None, ''), ('SPAWY-i', 'text', 'YES', '', None, ''), ('SPAWY_DAC-i', 'text', 'YES', '', None, ''), ('LOG-a', 'text', 'YES', '', None, ''), ('SPLIT1X4-i', 'text', 'YES', '', None, ''), ('SPLIT1X8-i', 'text', 'YES', '', None, ''), ('SPLIT1X16-i', 'text', 'YES', '', None, ''), ('ADAPTERY SIMPLE-i', 'double', 'YES', '', None, ''), ('ADAPTERY DUPLEX-i', 'double', 'YES', '', None, ''), ('PIGTAIL-i', 'double', 'YES', '', None, ''), ('OLT-a', 'double', 'YES', '', None, ''), ('NEXT-a', 'double', 'YES', '', None, ''), ('ODNOGA_1-a', 'double', 'YES', '', None, ''), ('ODNOGA_2-a', 'double', 'YES', '', None, ''), ('ODNOGA_3-a', 'double', 'YES', '', None, ''), ('ODNOGA_4-a', 'double', 'YES', '', None, ''), ('DROP30-i', 'double', 'YES', '', None, ''), ('DROP50-i', 'double', 'YES', '', None, ''), ('DROP70-i', 'double', 'YES', '', None, ''), ('DROP80-i', 'double', 'YES', '', None, ''), ('DROP100-i', 'double', 'YES', '', None, ''), ('DROP120-i', 'double', 'YES', '', None, ''), ('DROP150-i', 'double', 'YES', '', None, ''), ('DROP200-i', 'double', 'YES', '', None, ''), ('MAŁPKI-i', 'double', 'YES', '', None, '')]
And it lost ]
, so the next message if it will be converted to int so it can read how long next message will be it will just trow an error that for example ]6
is str.
I have complily no idea why this is happening. I arledy tried with diffrent table from databse and changed buffer size but it still eating somewhere this one peace.
And there are two prints in server. First show buffer that were used while receiving that. And second one for showing how many symbols it actually received and it always works but not with that string.
If I didn't cover something here or some important info, please write in comment and I will provide it.
CodePudding user response:
Your problem is that your production string contains a non-ASCII character, and that gets converted to multiple bytes when you encode to UTF-8. Those multiple bytes are not accounted for in your length transmission. You need to grab the length AFTER converting. So, get rid of the "send" function and do:
msg_to_send = msg_to_send.encode('utf-8')
for i in range(1000):
print(len(msg_to_send))
prefix = (str(len(msg_to_send)) "#"*30)[:30].encode('utf-8')
client.send(prefix msg_to_send)
while True:
time.sleep(5)