-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
97 lines (86 loc) · 3.17 KB
/
client.py
File metadata and controls
97 lines (86 loc) · 3.17 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
from help_function import download_chunk_function, setup_socket, download_content_length, get_file_links
import urllib
import io
import threading
lock = threading.Lock()
def handleConnection(URL, lock):
lock.acquire()
print("Thread start processing on: " + URL)
o = urllib.parse.urlsplit(URL)
BUFSIZE = 1024
HOST = o.netloc
socket = setup_socket.create_socket(URL)
client_type = setup_socket.get_client_type(URL)
# single main page
if (client_type == 1):
request = o.path
file_name = "index.html"
stri = f"GET {request} HTTP/1.1\r\nHost: {HOST}\r\nConnection: keep-alive\r\n\r\n"
socket.send(bytes(stri, "utf8"))
print("For the link " + URL)
download_option = input(
"What do you want to use (Content-length/ chunked): ")
if (len(download_option) > 0):
lock.release()
data = b""
data = socket.recv(BUFSIZE)
if (download_option == "Content-length"):
status, headers = download_content_length.dict_headers(
io.StringIO(data.decode(encoding="ISO-8859-1")))
download_content_length.download_content_length(
headers, BUFSIZE, data, socket, file_name)
else:
download_chunk_function.download_chunked(
data, socket, BUFSIZE, file_name)
# specific file
if (client_type == 2):
request = o.path
tmp, file_name = o.path.rsplit("/", 1)
stri = f"GET {request} HTTP/1.1\r\nHost: {HOST}\r\nConnection: keep-alive\r\n\r\n"
socket.send(bytes(stri, "utf8"))
print("For the link " + URL)
download_option = input(
"What do you want to use (Content-length/ chunked): ")
if (len(download_option) > 0):
lock.release()
data = b""
data = socket.recv(BUFSIZE)
if (download_option == "Content-length"):
status, headers = download_content_length.dict_headers(
io.StringIO(data.decode(encoding="ISO-8859-1")))
download_content_length.download_content_length(
headers, BUFSIZE, data, socket, file_name)
else:
download_chunk_function.download_chunked(
data, socket, BUFSIZE, file_name)
# folder
if (client_type == 3):
lock.release()
file_links = get_file_links.get_file_links(URL)
download_content_length.download_folder(
file_links, socket, BUFSIZE, HOST)
# MainProgram go here
if __name__ == '__main__':
# Please input the desired download link in the download_link.txt file
linkSet = set()
myfile = open("download_link.txt", "r")
while myfile:
line = myfile.readline()
if line == "":
break
if (ord(line[-1]) == 10):
lineFixed = line[:-1]
linkSet.add(lineFixed)
else:
linkSet.add(line)
myfile.close()
linkList = list(linkSet)
# Threading
threadList = []
lock = threading.Lock()
for link in linkList:
serverThread = threading.Thread(
target=handleConnection, args=(link, lock,))
threadList.append(serverThread)
serverThread.start()
[x.join() for x in threadList]