Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ПИ20-3 Карачик Э.И. #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<title>Edem's GitHub link</title>
<body>
<div>
<div>
<h1>This is an example web page for the Web Server task.</h1>
</div>

<body>
<p>
<h3><a href="https://github.com/EdemGit">My GitHub page</a></h3>
</p>
</body>
<div>
<h2>Completed by Edem Karachik</h2>
</div>
</div>
</body>
</html>
77 changes: 47 additions & 30 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import socket

sock = socket.socket()

try:
sock.bind(('', 80))
print("Using port 80")
except OSError:
sock.bind(('', 8080))
print("Using port 8080")

sock.listen(5)

conn, addr = sock.accept()
print("Connected", addr)

data = conn.recv(8192)
msg = data.decode()

print(msg)

resp = """HTTP/1.1 200 OK
Server: SelfMadeServer v0.0.1
Content-type: text/html
Connection: close

Hello, webworld!"""

conn.send(resp.encode())

conn.close()
import time
from threading import Thread


def start_server():
print('Starting the server...')
sock = socket.socket()
try:
sock.bind(('', 80))
print('Using port 80')
except OSError:
sock.bind(('', 8080))
print('Using port 8080')
sock.listen(5)
print('The server has launched')
while True:
conn, addr = sock.accept()
thread = Thread(target=proc, args=(conn, addr))
thread.start()


filename = 'index.html'


def proc(conn, addr):
h = (f'HTTP/1.1 200 OK\n'
f'Server: Apache/2.2.17\n'
f'Date: {time.asctime()}\n'
f'Content-Type: text/html\n'
f'Connection: close\n\n')
try:
user = conn.recv(1024).decode()
print(user)
path = user.split(' ')[1]
if path == '/':
with open(filename, 'rb') as f:
conn.send(h.encode('utf-8') + f.read())
else:
conn.send('HTTP/1.1 404\nNOT FOUND'.encode('utf-8'))
except IndexError:
conn.send('HTTP/1.1 404\nNOT FOUND'.encode('utf-8'))
print(addr, 'has connected')


if __name__ == '__main__':
filename = input('Enter the path (absolute or relative) to an existing .html file: ')
start_server()