Skip to content

srever.py updated #69

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 42 additions & 31 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
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 socket, time
from threading import Thread


def server():
print('Launching the server...')
sock = socket.socket()
try:
sock.bind(('', 80))
print("Using port 80")
except OSError:
sock.bind(('', 8080))
print("Using port 8080")
sock.listen(10)
print('Success!')
while 1:
conn, addr = sock.accept()
thread = Thread(target=proc, args=(conn, addr,))
thread.start()


name = input('Enter a full name of an existing .html file (Example: index.html): ')


def proc(conn, addr):
h = (f'HTTP/1.1 200 OK\n'
f'Server: SelfMadeServer v0.0.1\nDate: {time.asctime()}\nContent-Type: text/html\nConnection: close\n\n')
user = conn.recv(1024).decode()
path = user.split(" ")[1]
if path == '/':
with open(name, 'rb') as f:
answer = f.read()
conn.send(h.encode('utf-8') + answer)
else:
resp = """HTTP/1.1 404
NOT FOUND"""
conn.send(resp.encode('utf-8'))
print(addr, ' has connected')


if __name__ == "__main__":
server()