简体   繁体   中英

Send Data from Server to Client Python 2.7

I am trying to write a server that reads a .pdf file and sends the data to a client server.

Here is a snip of my server program.

while True: # infinite loop to connect to a client
    client, address = server.accept()
    data = copyfile(path)
    data = str(data)
    client.send(data.encode('utf-8'))
    client.close()

Here is a clip of my client source code:

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# assume my host and ports are correct
client.connect((host, port))
data = client.recv(1024)
s = data.decode('utf-8')

print s

client.close()

Here is the error I am gettting:

client.send(message.encode('utf-8'))

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 10: ordinal not in range(128)

Just send straight bytes.

#client.py
x = open('out.pdf','wb')
while 1:
    data =''
    data = s.recv(1024)
    if data == b'': break
    x.write(data)
x.close()

Server Code:

#server.py
x=open("test.pdf","rb")
client, address = s.accept()
while 1:
    data = x.read(1024)
    if data == b'':
        print("finished")
        x.close()
        client.close()
        break
    client.send(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM