Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #CLIENT
- import socket
- import sys
- host = '127.0.0.1'
- port = 5006
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- print('Connecting to server...')
- try:
- s.connect((host, port))
- print('Connected with server')
- print('---Welcome to the ZIP Extractor!---\nEnter the zip file location or press q to exit.')
- while True:
- data = input()
- if data == 'q' or data == 'Q':
- sys.exit()
- try:
- file = open('/home/kamil/D1.zip', 'rb')
- x = file.read()
- file.close()
- except IOError:
- print('File error')
- continue
- s.send(bytes("SIZE" + str(len(x)), 'utf-8'))
- print("SIZE", str(len(x)))
- data = s.recv(1024).decode('utf-8')
- if data == 'OK':
- s.send(bytes(x, 'utf-8'))
- data = s.recv(1024).decode('utf-8')
- if data == 'OK':
- print('success')
- else:
- print('failure')
- else:
- print('failure')
- s.close()
- except socket.error:
- s.close()
- print('Connection error')
- #SERVER
- import socket
- import sys, os
- from _thread import *
- import datetime
- import threading
- import shutil
- def getCurrentDateTime():
- return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:\t")
- host = '127.0.0.1'
- port = 5006
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- s.bind((host, port))
- except socket.error:
- print(getCurrentDateTime() + 'Running failed')
- sys.exit()
- s.listen(5)
- print(getCurrentDateTime() + 'Server is running...')
- def clientthread(conn):
- try:
- path = threading.get_ident()
- os.makedirs(str(path))
- except OSError:
- print('Directory creating error')
- while True:
- data = conn.recv(1024)
- if not data:
- print(getCurrentDateTime() + clientAddr + ' has disconnected.')
- shutil.rmtree(str(path))
- break
- data = data.decode('utf-8')
- print(data)
- if str(data[:4]) == 'SIZE':
- try:
- size = int(data[4:])
- if size < 1:
- raise Exception
- except:
- conn.send(bytes('FAILURE', 'utf-8'))
- else:
- conn.send(bytes('OK', 'utf-8'))
- data = conn.recv(1024)
- if not data:
- break
- data = data.decode()
- while len(data) < size:
- data += conn.recv(1024).decode('utf-8')
- print('Receiving...')
- conn.send(bytes('OK', 'utf-8'))
- path1 = str(path) + "/myfile.txt"
- print(str(data.decode('utf-8')))
- file = open(path1, 'w')
- file.write(data.split(b'\r\n', 1))
- file.close()
- print("OK")
- else:
- conn.send(bytes('SEND SIZE', 'utf-8'))
- conn.close()
- # print(getCurrentDateTime() + 'Received from ' + clientAddr + ': ' + reply)
- # conn.send(data)
- # print(getCurrentDateTime() + 'Send to ' + clientAddr + ': ' + data.decode())
- while True:
- conn, addr = s.accept()
- clientAddr = addr[0] + ':' + str(addr[1])
- print(getCurrentDateTime() + 'Connected with ' + clientAddr)
- start_new_thread(clientthread, (conn,))
- s.close()
- BÅÄD:
- Traceback (most recent call last):
- File "/home/kamil/PycharmProjects/KompresjaPlikow/Client.py", line 31, in <module>
- s.send(bytes(x, 'utf-8'))
- TypeError: encoding without a string argument
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement