בניית שרת HTTP - שלבים 5-6-7
בניית שרת HTTP - שלבים 5-6-7
בניית שרת HTTP - שלבים 5-6-7
אורי רוזנברג
:קוד
import socket
import os
SOCKET_TIMEOUT = 10
IP = "0.0.0.0"
PORT = 80
FIXED_RESPONSE = "Connected To Ori's Socket!\nProcessing Request..."
DEFAULT_URL = IP
def process_File(data):
path = data.split(' ')[1]
head_tail = os.path.split(path)
# return head_tail[1]
if (os.path.isfile(path)):
file = open(path, "rb")
file_data = file.read()
return file_data
else:
return "Error - No path found"
if resource == '':
url = DEFAULT_URL
else:
url = resource
return url
"""
if resource == '':
url = DEFAULT_URL
else:
url = resource
# TO DO: check if URL had been redirected, not available or other error
code. For example:
if url in REDIRECTION_DICTIONARY:
# TO DO: send 302 redirection response
# TO DO: extract requested file tupe from URL (html, jpg etc)
if filetype == 'html':
http_header = # TO DO: generate proper HTTP header
elif filetype == 'jpg':
http_header = # TO DO: generate proper jpg header
# TO DO: handle all other headers
def validate_http_request(data):
data_list = data.split('\r\n')
data_list2 = data_list[0].split(' ')
if((data_list2[0] == 'GET') & (data_list2[2] == 'HTTPS/1.1') &
(len(data_list) == 1)):
return True, process_File(data)
return False, False
def handle_client(client_socket):
""" Handles client requests: verifies client's requests are legal HTTP,
calls function to handle the requests """
print('Client connected')
while True:
data = client_socket.recv(1024).decode()
client_socket.send(FIXED_RESPONSE.encode())
valid_http, resource = validate_http_request(data)
if valid_http:
print('Got a valid HTTP request')
return_data = handle_client_request(resource, client_socket)
print(resource)
client_socket.sendall(resource)
break
else:
print('Error: Not a valid HTTP request')
break
"""
while True:
# TO DO: insert code that receives client request
# ...
valid_http, resource = validate_http_request(client_request)
if valid_http:
print('Got a valid HTTP request')
handle_client_request(resource, client_socket)
break
else:
print('Error: Not a valid HTTP request')
break
"""
print('Closing connection')
client_socket.close()
print("Closed connection")
def main():
# Open a socket and loop forever while waiting for clients
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((IP, PORT))
server_socket.listen()
print("Listening for connections on port {}".format(PORT))
while True:
client_socket, client_address = server_socket.accept()
print('New connection received')
client_socket.settimeout(SOCKET_TIMEOUT)
handle_client(client_socket)
if __name__ == "__main__":
main()
import socket
import os
SOCKET_TIMEOUT = 10
IP = "0.0.0.0"
PORT = 80
FIXED_RESPONSE = "Connected To Ori's Socket!\nProcessing
Request..."
DEFAULT_URL = "Version: HTTP 1.0\nResponse Code: 200"
# DEFAULT_URL = ""
def process_file(data):
print(data)
path = data.split(' ')[1]
print(path + "----------------------------")
head_tail = os.path.split(path)
# return head_tail[1]
path = r'C:\Users\oriro\PycharmProjects\Targil4' + path
if (os.path.isfile(path)):
file = open(path, "rb")
file_data = file.read()
file_size = (os.path.getsize(path))
return file_data, file_size
else:
return "Error - No path found".encode(), "Error -
No path found"
if resource == '':
url = "ERROR"
else:
url = resource
"""
if resource == '':
url = DEFAULT_URL
else:
url = resource
def validate_http_request(data):
if(data):
data_list = data.split('\r\n')
data_list2 = data_list[0].split(' ')
print(data_list2)
if((data_list2[0] == "GET") & (data_list2[2] ==
"HTTP/1.1")):
return True, data
return False, data
def handle_client(client_socket):
""" Handles client requests: verifies client's requests
are legal HTTP, calls function to handle the requests """
print('Client connected')
while True:
data = client_socket.recv(1024).decode()
# client_socket.send(FIXED_RESPONSE.encode())
valid_http, resource = validate_http_request(data)
if valid_http:
print('Got a valid HTTP request')
return_data = handle_client_request(resource,
client_socket)
client_socket.send(return_data)
else:
print('Error: Not a valid HTTP request')
break
"""
while True:
# TO DO: insert code that receives client request
# ...
valid_http, resource =
validate_http_request(client_request)
if valid_http:
print('Got a valid HTTP request')
handle_client_request(resource, client_socket)
break
else:
print('Error: Not a valid HTTP request')
break
"""
print('Closing connection')
client_socket.close()
print("------------Closed connection------------")
def main():
# Open a socket and loop forever while waiting for
clients
server_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server_socket.bind((IP, PORT))
server_socket.listen()
print("Listening for connections on port
{}".format(PORT))
while True:
client_socket, client_address =
server_socket.accept()
print('New connection received')
client_socket.settimeout(SOCKET_TIMEOUT)
handle_client(client_socket)
if __name__ == "__main__":
main()