Advertisement
Guest User

abc

a guest
Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. #CLIENT
  2. import socket
  3. import sys
  4.  
  5. host = '127.0.0.1'
  6. port = 5006
  7.  
  8. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. print('Connecting to server...')
  10. try:
  11.     s.connect((host, port))
  12.     print('Connected with server')
  13.     print('---Welcome to the ZIP Extractor!---\nEnter the zip file location or press q to exit.')
  14.  
  15.     while True:
  16.         data = input()
  17.         if data == 'q' or data == 'Q':
  18.             sys.exit()
  19.  
  20.         try:
  21.             file = open('/home/kamil/D1.zip', 'rb')
  22.             x = file.read()
  23.             file.close()
  24.         except IOError:
  25.             print('File error')
  26.             continue
  27.  
  28.         s.send(bytes("SIZE" + str(len(x)), 'utf-8'))
  29.         print("SIZE", str(len(x)))
  30.         data = s.recv(1024).decode('utf-8')
  31.         if data == 'OK':
  32.             s.send(bytes(x, 'utf-8'))
  33.             data = s.recv(1024).decode('utf-8')
  34.             if data == 'OK':
  35.                 print('success')
  36.             else:
  37.                 print('failure')
  38.         else:
  39.             print('failure')
  40.  
  41.     s.close()
  42. except socket.error:
  43.     s.close()
  44.     print('Connection error')
  45.  
  46. #SERVER
  47. import socket
  48. import sys, os
  49. from _thread import *
  50. import datetime
  51. import threading
  52. import shutil
  53.  
  54.  
  55. def getCurrentDateTime():
  56.     return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:\t")
  57.  
  58.  
  59. host = '127.0.0.1'
  60. port = 5006
  61.  
  62. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  63.  
  64. try:
  65.     s.bind((host, port))
  66. except socket.error:
  67.     print(getCurrentDateTime() + 'Running failed')
  68.     sys.exit()
  69.  
  70. s.listen(5)
  71. print(getCurrentDateTime() + 'Server is running...')
  72.  
  73.  
  74. def clientthread(conn):
  75.     try:
  76.         path = threading.get_ident()
  77.         os.makedirs(str(path))
  78.     except OSError:
  79.         print('Directory creating error')
  80.  
  81.     while True:
  82.         data = conn.recv(1024)
  83.         if not data:
  84.             print(getCurrentDateTime() + clientAddr + ' has disconnected.')
  85.             shutil.rmtree(str(path))
  86.             break
  87.  
  88.         data = data.decode('utf-8')
  89.         print(data)
  90.         if str(data[:4]) == 'SIZE':
  91.             try:
  92.                 size = int(data[4:])
  93.                 if size < 1:
  94.                     raise Exception
  95.             except:
  96.                 conn.send(bytes('FAILURE', 'utf-8'))
  97.             else:
  98.                 conn.send(bytes('OK', 'utf-8'))
  99.                 data = conn.recv(1024)
  100.                 if not data:
  101.                     break
  102.                 data = data.decode()
  103.                 while len(data) < size:
  104.                     data += conn.recv(1024).decode('utf-8')
  105.                     print('Receiving...')
  106.                 conn.send(bytes('OK', 'utf-8'))
  107.  
  108.                 path1 = str(path) + "/myfile.txt"
  109.                 print(str(data.decode('utf-8')))
  110.                 file = open(path1, 'w')
  111.                 file.write(data.split(b'\r\n', 1))
  112.                 file.close()
  113.                 print("OK")
  114.         else:
  115.             conn.send(bytes('SEND SIZE', 'utf-8'))
  116.  
  117.  
  118.     conn.close()
  119.  
  120.         # print(getCurrentDateTime() + 'Received from ' + clientAddr + ': ' + reply)
  121.         # conn.send(data)
  122.         # print(getCurrentDateTime() + 'Send to ' + clientAddr + ': ' + data.decode())
  123.  
  124.  
  125.  
  126.  
  127. while True:
  128.     conn, addr = s.accept()
  129.     clientAddr = addr[0] + ':' + str(addr[1])
  130.     print(getCurrentDateTime() + 'Connected with ' + clientAddr)
  131.     start_new_thread(clientthread, (conn,))
  132.  
  133. s.close()
  134.  
  135.  
  136. BŁĄD:
  137. Traceback (most recent call last):
  138.   File "/home/kamil/PycharmProjects/KompresjaPlikow/Client.py", line 31, in <module>
  139.     s.send(bytes(x, 'utf-8'))
  140. TypeError: encoding without a string argument
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement