-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
67 lines (52 loc) · 1.48 KB
/
client.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
"""
Carlos Bustos
1001317137
Project 1
"""
from socket import *
import sys
#import urllib.request
#making a client
clientSocket = socket(AF_INET, SOCK_STREAM)
if(len(sys.argv) == 4):
hostName = sys.argv[1]
port = int(sys.argv[2])
file = sys.argv[3]
else:
hostName = sys.argv[1]
port = 80
file = sys.argv[2]
try:
#Connect the client socket to the host.
clientSocket.connect((hostName, port))
print('\nConnecting...\n')
except:
print("\nHost name invalid.\n")
sys.exit(1)
print("Socket Information:")
print(clientSocket)
"""
url = 'http://'+hostName+'/'+file
with urllib.request.urlopen(url) as response:
html = response.read()
"""
#Prompt the user to enter the path of the file/page.
filePath = input("Please specify the file path:")
#Create a HTTP GET request to retrieve the file.
request = "GET "+filePath+" HTTP 1.1\n\n"
#Send to server
clientSocket.send(str.encode(request))
result = clientSocket.recv(4096)
if(len(result) == 0):
print("\nNothing was received from the server.\n\n")
#This loop will recieve all the data from the server and display it.
while (len(result) > 0):
try:
result.decode()
print(result)
result = clientSocket.recv(4096)
except UnicodeDecodeError:
print("Error in decoding file to Unicode.")
break
clientSocket.close() #Close the socket and inform the user.
print("\nConnection is now closed.\n\n")