Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
232 lines (191 loc) · 6.55 KB
/
Copy pathclient.py
File metadata and controls
232 lines (191 loc) · 6.55 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import json
import os
import ssl
# from sendfile import sendfile
TCP_IP = '172.17.0.2'
TCP_PORT = 30000
BUFFER_SIZE = 1024
class Client:
def __init__(self, sock=None):
# self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.create_default_context()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_verify_locations("example.crt")
if sock is None:
aux = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s = context.wrap_socket(aux, server_hostname=TCP_IP)
else:
self.s = context.wrap_socket(sock, server_hostname=TCP_IP)
# Sendo data fron server.
def sendCommandServer(self, command):
self.s.send(command.encode())
def recevCommandServer(self):
return_server = self.s.recv(BUFFER_SIZE).decode()
self.s.close()
return return_server
def clientThreadConnect(self):
self.s.connect((TCP_IP, TCP_PORT))
def clientReconnect(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def clientThreadCloseConection(self):
self.s.close()
def send(self, msg):
totalsent = 0
MSGLEN = len(msg)
# print(msg)
while totalsent < MSGLEN:
sent = self.s.send(msg[totalsent:].encode('utf-8'))
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
def receive(self, EOFChar='\036'):
msg = ''
MSGLEN = 100
while len(msg) < MSGLEN:
chunk = self.s.recv(MSGLEN-len(msg)).decode('utf-8')
if chunk.find(EOFChar) != -1:
msg = msg + chunk
# print(chunk)
return msg
# print(chunk)
msg = msg + chunk
return msg
def sendArquive(self, filename):
f = open(filename, 'rb')
self.s.sendfile(f)
print('Successfully put the file')
print ("====================================")
print ("")
print ("Iniciando client.... ")
print ("")
# print (socket.gethostbyaddr('10.13.239.210'))
client = Client()
# conect client
client.clientThreadConnect()
# send commando for autenticate
client.send("user")
# receive return
return_server = client.receive()
if return_server == "ok":
filename = "test.js"
fileDependencies = "pack.json"
# send file for dependencies
client.sendArquive(fileDependencies)
# send file for execution
client.sendArquive(filename)
# receive return
return_server = client.receive()
print(return_server)
# close connection
client.clientThreadCloseConection()
else:
print(return_server)
client.clientThreadCloseConection()
# quit
quit = 1
while quit != 1:
print ("====================================")
print ("")
print ("Chose intstuction:")
print ("")
print ("USER - user myfile.json")
print ("SEND - send myfile.js")
print ("RUN - run")
print ("QUIT - quit")
print ("")
print ("====================================")
print ("")
entrance = input("Digit your option: ")
print ("")
# get and validate user command
if entrance[:5] == "user ":
# open connect with server
client.clientThreadConnect()
# test if the file is json
if entrance[len(entrance)-5:] == ".json":
print ("Login in server...")
print ("")
user_login = entrance[5:]
print ("====================================")
print ("")
print ("File sent: ", user_login)
print ("")
# autenthicate user with user.json
# revise
with open(user_login) as f:
json_dict = json.load(f)
users = json_dict["userlist"]
for item in users:
if item["usuario"] == "grupo01" and item["senha"] == "01234":
print("grupo01 autenticado")
elif item["usuario"] == "grupo02" and item["senha"] == "12345":
print("grupo02 autenticado")
elif item["usuario"] == "grupo03" and item["senha"] == "23456":
print ("grupo03 autenticado")
elif item["usuario"] == "grupo04" and item ["senha"] == "34567":
print ("grupo04 autenticado")
else:
print ("")
print ("User or passwor invelid")
print ("")
else:
print("[ERROR 303] - format invalid.")
# SEND
elif entrance[:5] == "send ":
# send name file for server
entrance = "send test.js"
#print("valida 1")
client.sendCommandServer(entrance[:5])
#print("valida 2")
validate = client.recevCommandServer()
validate = "ok"
# validate file
#print("valida 3")
if validate == "ok":
#print("valida 3")
if entrance[len(entrance)-3:] == ".js":
# get namo for validation
filename = entrance[5:]
print (filename)
# get information file
filestat = os.stat(filename)
filesize = filestat.st_size
print ("This file has", filesize, "bytes.")
# test file size
if filesize > 3000000:
print ("The file is too large.")
else:
print ("Size correct.")
client.clientThreadSendFile(filename)
# client.clientThreadCloseConection()
# RUN
elif entrance == "run":
print ("====================================")
print ("")
print ("Trying to run application...")
print ("")
client.sendCommandServer("run")
# print("parou 1")
client.recevCommandServer()
# print("parou 2")
# insert return here
# QUIT
elif entrance == "quit":
print ("====================================")
print ("")
print ("Closing client!")
print ("")
print ("====================================")
client.sendCommandServer("quit")
client.clientThreadCloseConection()
quit = 1
else:
print ("====================================")
print ("")
print ("inválid command")
print ("")