-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
47 lines (35 loc) · 1.48 KB
/
server.py
File metadata and controls
47 lines (35 loc) · 1.48 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
import urllib.request
import socket
import os
links=['https://stackoverflow.com','https://www.wikipedia.org','https://www.reddit.com','https://www.yahoo.com','https://www.twitch.tv','https://www.linkedin.com','https://www.instagram.com','https://www.github.com']
for i in links:
html_res = urllib.request.urlopen(i)
html_content = html_res.read()
i=i.replace('https://','')
name="serverFile/"+i+".html"
file = open(name, 'w',encoding='utf-8')
file.write(html_content.decode())
file.close()
files=os.listdir('serverFile')
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65435 # Port to listen on (non-privileged ports are > 1023)
FileFounded=False
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
print('server is runnig...')
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024).decode()
if not data:
break
for file in files:
if data + ".html" == file:
FileFounded=True
conn.send('200'.encode())
with open('serverFile/'+file,'rb') as client_file:
conn.sendfile(client_file)
if not FileFounded:
s.sendall('404NOTFOUNDED'.encode())