-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
131 lines (116 loc) · 4.55 KB
/
Copy pathclient.py
File metadata and controls
131 lines (116 loc) · 4.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
from socket import *
import sys
import time
from threading import Thread
import ast
import os
if len(sys.argv) != 2:
print("\n===== Error usage, python3 TCPClient3.py SERVER_IP SERVER_PORT ======\n")
exit(0)
serverPort = int(sys.argv[1])
serverAddress = ('127.0.0.1', serverPort)
# define a socket for the client side, it would be used to communicate with the server
# the sending port is automatically picked from one of the free ports so no need to bind
clientSocket = socket(AF_INET, SOCK_DGRAM)
# define a listejning socket for the client to client connection
clientTCPListeningSocket = socket(AF_INET, SOCK_STREAM)
# I made the TCP listening port automatically assigned to free port using 0
clientTCPListeningSocket.bind(('127.0.0.1', 0))
listeningSocketAddress = clientTCPListeningSocket.getsockname()
clientTCPListeningSocket.listen(10)
def newTCPconnections(clientTCPListeningSocket):
while 1:
connectionSocket, addr = clientTCPListeningSocket.accept()
#print(f"New connection from {addr}")
sentence = connectionSocket.recv(1024)
message = sentence.decode('utf-8')
parts = message.split("/", 1)
message = parts[0]
userName = parts[1].strip()
if message[:4] == "get:":
file_path = os.path.join(os.getcwd(), userName, message[4:])
with open(file_path, 'rb') as f:
#print("Sending file...")
while True:
file_data = f.read(1024)
if not file_data:
connectionSocket.close()
break
connectionSocket.send(file_data)
def heartbeat():
while True:
message = "heartbeat"
clientSocket.sendto(message.encode('utf-8'), serverAddress)
time.sleep(2)
def requestFile(message):
parts = message.split("/", 3)
# Extract clientAddress
clientAddressPart = parts[0]
# extract filename
file = parts[1].strip()
# extract user with file username
userName = parts[2].strip()
#extract current user who wants file
curr = parts[3].strip()
# Further split to get the actual address value
clientAddress = ast.literal_eval(clientAddressPart.split(":")[1])
clientTCPSocket = socket(AF_INET, SOCK_STREAM)
clientTCPSocket.connect(clientAddress)
message = "get:" + file + "/" + userName
clientTCPSocket.send(message.encode())
# Open a file to write the received data to
file_path = os.path.join(os.getcwd(), curr, file)
with open(file_path, 'wb') as f:
while True:
#print("entered this loop")
data = clientTCPSocket.recv(1024)
if not data:
#print("we finally exiting")
break
#print("writing this shit up: "+ data.decode())
f.write(data)
print(file + " downloaded successfully")
def primary():
print("Welcome to BitTrickle!")
print("Available commands are: get, lap, lpf, pub, sch, unp, xit")
while 1:
message = input("> ")
if message == "xit":
print("Goodbye!")
break
clientSocket.sendto(message.encode('utf-8'), serverAddress)
data = clientSocket.recv(1024)
receivedMessage = data.decode()
if receivedMessage[:26] == "clientTCPListeningAddress:":
requestFile(receivedMessage)
else:
print(receivedMessage)
# log in credentials loop
unverified = 1
while unverified:
userName = input("Enter username: ")
userPassword = input("Enter password: ")
userLogin = "Login:" + userName + " " + userPassword + " " + str(listeningSocketAddress)
# print(userLogin)
clientSocket.sendto(userLogin.encode('utf-8'), serverAddress)
data = clientSocket.recv(1024)
loginConfirmation = data.decode()
if loginConfirmation == '1':
unverified = 0
if not os.path.exists(userName):
# print("created folder")
os.makedirs(userName)
else:
print("Authentication failed. Please try again.")
# put heartbeat onto a seperate thread
heartbeat_thread = Thread(target=heartbeat)
heartbeat_thread.daemon = True
heartbeat_thread.start()
# Put newTCPconnections onto a separate thread
tcp_listener_thread = Thread(target=newTCPconnections, args=(clientTCPListeningSocket,))
tcp_listener_thread.daemon = True
tcp_listener_thread.start()
# start primary program
primary()
clientSocket.close()
clientTCPListeningSocket.close()