0

Good morning, I've a simple question. I need a socket that allow a client to send an XML to a server. I've tried to send a simple string b'simple string just for example' and everything works fine, but when I try to send an XML (as binary) the server isn't able to write that down. I'll paste my code

Client

import socket

socket = socket.socket()
socket.connect(("localhost", 54610))
file = open(r"c:\shared\68118.xml", "rb")
stream = file.read(65536)
while stream:
    socket.send(stream)
    stream = file.read(65536) # tried with 1024, 2048
socket.close()

Server

import socket

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('', 54610)
socket.bind(server_address)
socket.listen(1)

while True:
    inbound_stream, address = socket.accept()

    print(address)
    counter = 1
    file = open(r'c:\shared\68118_' + str(counter) + '.xml', 'wb')  # binary
    counter += 1
    while(True):
        stream = inbound_stream.recv(65536)
        while stream:
            file.write(stream)
            stream = inbound_stream.recv(65536) # tried with 1024, 2048
        file.close()
        inbound_stream.close()

socket.close() # TODO NOT REACHABLE

the output file is 68118_1.xml 0Kb with nothing inside

What I'm doing wrong?

Thank you in advance

1 Answer 1

0

I've just remove the stream = file.read(65536) # tried with 1024, 2048 from the loop and everything worked fine

Sign up to request clarification or add additional context in comments.

1 Comment

Probably because your file is less than 64kB. But that's not generic. Check stackoverflow.com/questions/30615187/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.