Send image using socket programming Python

后端 未结 2 1038
庸人自扰
庸人自扰 2020-12-14 04:02

I am trying to send an image file using socket programming in python. I am able to send a text file. But I have been trying to send an image file, by opening it and reading

2条回答
  •  轮回少年
    2020-12-14 04:59

    I ran the same code on my system (Ubuntu 11.10) and I found that, there is a problem with sending size as a string. When I inserted logic to handle that part, it ran smoothly. I could open the file also. This is how I could solve your problem : 1st change in client code(client.py) is while accepting size and sending acknowledgement about it :-

    size = ' '
    while(1):
       tmpsize = client_socket.recv(1)
       if tmpsize.isdigit() == True:
          print "Here : ",tmpsize
          size += tmpsize
       else:
          break
    
    client_socket.send("received")
    

    2nd change is in server side(server.py) code, to accept acknowledgement :-

    client_socket.send(size)
    ack = client_socket.recv(1024)
    if ack == "received":
       client_socket.send (strng)
    

    I hope this will help you to solve your problem.

提交回复
热议问题