-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
74 lines (62 loc) · 3.14 KB
/
Copy pathClient.py
File metadata and controls
74 lines (62 loc) · 3.14 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
# Import socket library as well as the datetime
import socket
from datetime import datetime
# Client program, working the client side of the client/server architecture, the client will
# make a request to the server for the html file contents, and once the connection is established
# and the file name is added, the server will return the HTML file contents for it to be changed
def clientProgram():
# Get the host name and establish the port number the client/server will communicate on
host = socket.gethostname()
port = 8000
# Instance of the socket object, and the connect function call, using host and port number
# finally, once connection is established, print that the connection is established
clientSocket = socket.socket()
clientSocket.connect((host, port))
print('Connection Established...')
# The client will prompt to enter a value, this will continue to ask until the name of the
# HTML file contents are to be downloaded, finally data received is saved as a string
message = input(' -> ')
clientSocket.send(message.encode())
data = clientSocket.recv(1024).decode()
# While the user does not enter in the HTML file name, the file does not exist, so the
# error message will be returned each time, until the user enters in 'IPAddress.html'
# Call send function with the message input by the client through the client socket
# finally, once the HTML file contents are transferred successfully, call makeHTML()
while (data == '--> ERROR: File not found'):
print('--> ERROR: File not found')
message = input(' -> ')
clientSocket.send(message.encode())
data = clientSocket.recv(1024).decode()
makeHTML(data)
# Once the HTML file contents are added, close the connection to the server file
print('File successfully transfered...')
print('Connection Terminated... ')
clientSocket.close()
# The file contents transferred over the clientsocket are then input into makeHTML()
def makeHTML(contents):
# Make a list of the split lines from the content of the file content transfer
# and the iterate the list until the </body> is found, then break
list = contents.splitlines()
for i in range(0, len(list)):
if list[i] == '</body>':
break
# Now is the date and time of right now according to the computer internal clock
# then the messages are added to the HTML file contents, according to list index above
now = datetime.now()
ipAddress = "\t<p>My IP Address is " + str(socket.gethostbyname(socket.gethostname())) + "</p>"
date = "\t<p>Date and Time is " + str(now) + "</p>"
# Insert both into the list, and the join the list together with the join function,
# finally write the changes to the final.html file
list.insert(i, date)
list.insert(i, ipAddress)
result = '\n'.join(list)
writeHTML(result)
# Writes the contents from the makeHTML() function to another html file
# that is updated to include date/time and IP address of host
def writeHTML(contents):
file = open("final.html", "w")
file.write(contents)
file.close()
# Main function
if __name__ == '__main__':
clientProgram()