Advertisement
Guest User

Untitled

a guest
Aug 6th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #! /usr/bin/python3
  2. # -*- python -*-
  3. # -*- coding: utf-8 -*-
  4.  
  5.  
  6. import socket
  7. import _thread
  8.  
  9.  
  10. fn = '/home/pc/soft/OS/ubuntu-18.04.1-desktop-amd64.iso'
  11. lock = _thread.allocate_lock()
  12.  
  13.  
  14. def printl(s):
  15.     lock.acquire()
  16.     print(s)
  17.     lock.release()
  18.  
  19.  
  20. def dl(client_socket, addr):
  21.     try:
  22.         printl(f'sending file to {addr}')
  23.         with open(fn, 'rb') as f:
  24.             while True:
  25.                 #printl(CHUNK_SIZE) тут оно пишет правильную цифру и вообще все работает как задумано
  26.                 data = f.read(CHUNK_SIZE)
  27.                 if not data: break
  28.                 client_socket.send(data)
  29.         printl(f'complete to {addr}')
  30.     except ConnectionResetError:
  31.         printl(f'aborted to {addr}')
  32.     client_socket.close()
  33.  
  34.  
  35. if __name__ == '__main__':
  36.     CHUNK_SIZE = 128 * 1024
  37.     server_socket = socket.socket()
  38.     server_socket.bind(('', 12345))
  39.     server_socket.listen(5)
  40.     while True:
  41.         client_socket, addr = server_socket.accept()
  42.         _thread.start_new_thread(dl, (client_socket, addr))
  43.     server_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement