Advertisement
obernardovieira

Send binary files via Socket

Mar 7th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. //server.py
  2.  
  3. #!/usr/bin/python
  4.  
  5. import socket
  6. import sys
  7.  
  8. s = socket.socket()
  9. host = socket.gethostname()
  10. port = 12345
  11. s.bind((host, port))
  12.  
  13. s.listen(5)
  14. while True:
  15.     sc, address = s.accept()
  16.  
  17.     print address
  18.     i=1
  19.     f = open('file_'+ str(i)+".png",'wb')
  20.     i=i+1
  21.     while (True):
  22.         l = sc.recv(1024)
  23.         while (l):
  24.                 f.write(l)
  25.                 l = sc.recv(1024)
  26.     f.close()
  27.  
  28.  
  29.     sc.close()
  30.  
  31. s.close()
  32.  
  33.  
  34. //client.py
  35.  
  36. #!/usr/bin/python
  37.  
  38. import socket
  39. import sys
  40.  
  41. s = socket.socket()
  42. host = socket.gethostname()
  43. port = 12345
  44.  
  45. s.connect((host, port))
  46. f=open ("test.png", "rb")
  47. l = f.read(1024)
  48. while (l):
  49.     s.send(l)
  50.     l = f.read(1024)
  51. s.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement