-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_helperfunctions.py
122 lines (88 loc) · 3.74 KB
/
client_helperfunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from socket import *
from sys import getsizeof
import time
import gzip
# ============================== helper =================================
def send_file(clientSocket, fileName):
file = open(fileName, 'rb')
file_contents = file.read()
file.close()
size = str(getsizeof(file_contents))
# sending file size
clientSocket.send(bytes(size, 'utf-8'))
# sending file
clientSocket.send(file_contents)
# ============================== client =================================
#search word function
def search_word(clientSocket):
'''
search word function (client):
> reads file and sends the client each word, then upon recieving the count, displays it
'''
filename, word = input('input the file to search through and word to search (space-separated): ').split()
# sending word to search
clientSocket.send(bytes(word, 'utf-8'))
# checks if filename has .txt at the end
if (filename[-4:] != '.txt'):
filename = filename + '.txt'
if (filename):
send_file(clientSocket, filename)
count = int(clientSocket.recv(1024).decode())
print('\nFound {} {} times in {}\n'.format(word, count, filename))
else:
print('No file given')
#replace word function
def replace_word(clientSocket):
'''
replace word function (client):
> reads file and sends the client each word, then upon recieving the count, displays it
'''
word_to_replace, replace_to = input("Please enter the word you'd like to replace, then the word you want to replace to (space-separated): ").split()
input_file, output_file = input('Input the file to search through and file name to save to (space-separated): ').split()
words_to_send = [word_to_replace, replace_to]
print('DEBUG: ', words_to_send)
# sending word to replace, replace to to server
for i in words_to_send:
clientSocket.send(bytes(i, 'utf-8'))
if (input_file[-4:] != '.txt'):
input_file = input_file + '.txt'
send_file(clientSocket, input_file)
size = int(clientSocket.recv(1024).decode())
new_file_content = str(clientSocket.recv(size).decode())
new = open(output_file, 'w+')
new.write(new_file_content)
new.close()
print('file created')
#display file function
def display_file(clientSocket):
filename = input('Input the file to display: ')
if (filename[-4:] != '.txt'):
filename = filename + '.txt'
display = ''
with open(filename, "r") as file:
for line in file:
for each_word in line.split():
display += ' '
display += each_word
print('File Contents:', '\n', display, '\n')
#reverse word function
def reverse_word(clientSocket):
input_file, output_file = input('Input the file to search through and file name to save to (space-separated): ').split()
clientSocket.send(bytes(output_file, 'utf-8'))
with open(input_file, "r") as file:
for line in file:
for each_word in line.split():
clientSocket.send(bytes(each_word, 'utf-8'))
clientSocket.send(bytes('FINSH_SENDING_TO_CLIENT', 'utf-8'))
print(output_file, 'has been created')
#exit function
def Exit(clientSocket):
clientSocket.send(bytes('Exit', 'utf-8'))
clientSocket.shutdown(SHUT_RDWR)
clientSocket.close()
exit()
#displaying the thread active when the client connects to the server
def get_thread_name(clientSocket, userInput):
clientSocket.send(bytes(userInput, 'utf-8'))
thread_connected = clientSocket.recv(1024).decode()
print('Client connected to thread:', thread_connected)