SlideShare a Scribd company logo
Python, fill in code are marked with #FillInStart and #FillInEnd
import socket
import sys
import os
# Since the server might send the HTML across more than one
# send, we should try multiple recv calls to receive all data
def recvall(sock):
BUFFER_SIZE = 4096 # 4 KiB
data = b""
while True:
part = sock.recv(BUFFER_SIZE)
data += part
# Keep receiving only until the received data "ended" in last recv
if len(part) < BUFFER_SIZE:
break
return data
# Create a server socket, bind it to a port and start listening
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# FillInStart
# FillInEnd
print('Ready to serve...')
while True:
# accept a connection from the client side and decode the message/request
clientSide, addr = listener.accept()
message = #FillInStart #FillInEnd
# for now, lets ignore all the requests that are not GET
# the remaining requests will just be discarded and the client
# will receive no response for those requests
if not message.startswith("GET"):
continue
# extract the URL, domain, method, path from the request
request_method = message.split()[0]
requested_url = message.split()[1]
domain = requested_url.split("/", 3)[2]
path = requested_url.split("/", 3)[3]
# if the path ends in / then we can name it as index.html since
# is usually the case across the web
if path == "":
path = "index.html"
print("request method: {}, requested URL: {} -> domain: {}, path:
{}".format(request_method, requested_url, domain, path))
# create the directory structure similar to how the paths
# are defined within the URL. So, the parent folder will be the
# domain you are visiting and the path will be converted into
# subdirectories within the parent folder for intuitive navigation
file_path = "./{}/{}".format(domain, path)
file_exists = os.path.exists(file_path)
if file_exists:
# if the file exists, we can just read it and send it
print("file was found in the local cache at the proxy")
f = open(file_path, "rb")
outputdata = f.read()
else:
# otherwise, we need to make the request to the remote server
# to retrieve the file and send it to client while also saving
# it locally for future visits
print("file was NOT found in the local cache at the proxy")
print("requesting from server...")
# the GET request is made on HTTP/1.0 so that we don't need to
# deal with gzip compression and the response handling is fairly
# straightforward
GET_request = ""
GET_request += "GET {} HTTP/1.0rn".format(requested_url)
GET_request += "Host: {}rnrn".format(domain)
print(GET_request)
# we open the socket to the server and make the request at port 80
# FillInStart
# FillInEnd
# using the port you have created above to send the GET request
serverSide.send(GET_request.encode())
# receive the response using recvall in case the response takes more
# than one send by the server to send
GET_response = recvall(serverSide)
print("R: {}".format(GET_response))
# we create the directory and subdirectories in case they don't
# don't exist already and save the response as a file there
file_dir = file_path.rsplit("/", 1)[0]
if not os.path.exists(file_dir):
os.makedirs(file_dir)
with open(file_path, "wb") as f:
f.write(GET_response)
# we are also going to send this response to the client
outputdata = GET_response
# send the outputdata (GET response) back to the client
# FillInStart
# FillInEnd
print("data sent")
listener.close()

More Related Content

Similar to Python, fill in code are marked with #FillInStart and #FillInEndim.pdf

6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
Piyush Kumar
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
EloAcubaOgardo
 
Bleeding secrets
Bleeding secretsBleeding secrets
Bleeding secrets
Ofer Rivlin, CISSP
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
Kristian Arjianto
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docxRunning Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
cowinhelen
 
Devry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-filesDevry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-files
cskvsmi44
 
Devry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-filesDevry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-files
noahjamessss
 
Linux administration ii-parti
Linux administration ii-partiLinux administration ii-parti
Linux administration ii-parti
Sehla Loussaief Zayen
 
Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)
dantleech
 
part2
part2part2
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
Youssef Dirani
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
Youssef Dirani
 
Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3
HyeonSeok Choi
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORS
RapidValue
 
Netkitmig
NetkitmigNetkitmig
Netkitmig
renyufei
 
123
123123
Concepts unix process
Concepts unix processConcepts unix process
Concepts unix process
Yogesh Kumar
 
Web
WebWeb
Web
googli
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
Maulik Borsaniya
 

Similar to Python, fill in code are marked with #FillInStart and #FillInEndim.pdf (20)

6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
6. reverse primarydns using bind for ptr and cname record ipv6 with forwarder
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
Bleeding secrets
Bleeding secretsBleeding secrets
Bleeding secrets
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docxRunning Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
 
Devry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-filesDevry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-files
 
Devry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-filesDevry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-files
 
Linux administration ii-parti
Linux administration ii-partiLinux administration ii-parti
Linux administration ii-parti
 
Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)Exploring Async PHP (SF Live Berlin 2019)
Exploring Async PHP (SF Live Berlin 2019)
 
part2
part2part2
part2
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3Nodejs 프로그래밍 ch.3
Nodejs 프로그래밍 ch.3
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORS
 
Netkitmig
NetkitmigNetkitmig
Netkitmig
 
123
123123
123
 
Concepts unix process
Concepts unix processConcepts unix process
Concepts unix process
 
Web
WebWeb
Web
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 

More from agmobiles

Question 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdfQuestion 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdf
agmobiles
 
Question Please provide the links or the references where the info.pdf
Question  Please provide the links or the references where the info.pdfQuestion  Please provide the links or the references where the info.pdf
Question Please provide the links or the references where the info.pdf
agmobiles
 
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdfQN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
agmobiles
 
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdfQS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
agmobiles
 
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdfq60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
agmobiles
 
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdfQ4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
agmobiles
 
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdfQ1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
agmobiles
 
Put this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdfPut this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdf
agmobiles
 
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdfProyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
agmobiles
 
Provide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdfProvide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdf
agmobiles
 
public class AVLTreeT extends ComparableT extends BSTT { p.pdf
public class AVLTreeT extends ComparableT extends BSTT {   p.pdfpublic class AVLTreeT extends ComparableT extends BSTT {   p.pdf
public class AVLTreeT extends ComparableT extends BSTT { p.pdf
agmobiles
 
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdfProvide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
agmobiles
 
Prove (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdfProve (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdf
agmobiles
 
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdfProject Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
agmobiles
 
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdfProductos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
agmobiles
 
Process Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdfProcess Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdf
agmobiles
 
programming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdfprogramming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdf
agmobiles
 
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdfProblema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
agmobiles
 
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdfPreguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
agmobiles
 
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdfProblem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
agmobiles
 

More from agmobiles (20)

Question 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdfQuestion 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdf
 
Question Please provide the links or the references where the info.pdf
Question  Please provide the links or the references where the info.pdfQuestion  Please provide the links or the references where the info.pdf
Question Please provide the links or the references where the info.pdf
 
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdfQN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
 
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdfQS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
 
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdfq60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
 
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdfQ4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
 
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdfQ1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
 
Put this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdfPut this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdf
 
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdfProyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
 
Provide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdfProvide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdf
 
public class AVLTreeT extends ComparableT extends BSTT { p.pdf
public class AVLTreeT extends ComparableT extends BSTT {   p.pdfpublic class AVLTreeT extends ComparableT extends BSTT {   p.pdf
public class AVLTreeT extends ComparableT extends BSTT { p.pdf
 
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdfProvide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
 
Prove (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdfProve (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdf
 
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdfProject Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
 
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdfProductos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
 
Process Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdfProcess Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdf
 
programming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdfprogramming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdf
 
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdfProblema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
 
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdfPreguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
 
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdfProblem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
 

Recently uploaded

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 

Python, fill in code are marked with #FillInStart and #FillInEndim.pdf

  • 1. Python, fill in code are marked with #FillInStart and #FillInEnd import socket import sys import os # Since the server might send the HTML across more than one # send, we should try multiple recv calls to receive all data def recvall(sock): BUFFER_SIZE = 4096 # 4 KiB data = b"" while True: part = sock.recv(BUFFER_SIZE) data += part # Keep receiving only until the received data "ended" in last recv if len(part) < BUFFER_SIZE: break return data # Create a server socket, bind it to a port and start listening listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # FillInStart # FillInEnd print('Ready to serve...') while True: # accept a connection from the client side and decode the message/request clientSide, addr = listener.accept() message = #FillInStart #FillInEnd # for now, lets ignore all the requests that are not GET # the remaining requests will just be discarded and the client # will receive no response for those requests if not message.startswith("GET"): continue # extract the URL, domain, method, path from the request request_method = message.split()[0] requested_url = message.split()[1] domain = requested_url.split("/", 3)[2] path = requested_url.split("/", 3)[3]
  • 2. # if the path ends in / then we can name it as index.html since # is usually the case across the web if path == "": path = "index.html" print("request method: {}, requested URL: {} -> domain: {}, path: {}".format(request_method, requested_url, domain, path)) # create the directory structure similar to how the paths # are defined within the URL. So, the parent folder will be the # domain you are visiting and the path will be converted into # subdirectories within the parent folder for intuitive navigation file_path = "./{}/{}".format(domain, path) file_exists = os.path.exists(file_path) if file_exists: # if the file exists, we can just read it and send it print("file was found in the local cache at the proxy") f = open(file_path, "rb") outputdata = f.read() else: # otherwise, we need to make the request to the remote server # to retrieve the file and send it to client while also saving # it locally for future visits print("file was NOT found in the local cache at the proxy") print("requesting from server...") # the GET request is made on HTTP/1.0 so that we don't need to # deal with gzip compression and the response handling is fairly # straightforward GET_request = "" GET_request += "GET {} HTTP/1.0rn".format(requested_url) GET_request += "Host: {}rnrn".format(domain) print(GET_request) # we open the socket to the server and make the request at port 80 # FillInStart # FillInEnd # using the port you have created above to send the GET request serverSide.send(GET_request.encode())
  • 3. # receive the response using recvall in case the response takes more # than one send by the server to send GET_response = recvall(serverSide) print("R: {}".format(GET_response)) # we create the directory and subdirectories in case they don't # don't exist already and save the response as a file there file_dir = file_path.rsplit("/", 1)[0] if not os.path.exists(file_dir): os.makedirs(file_dir) with open(file_path, "wb") as f: f.write(GET_response) # we are also going to send this response to the client outputdata = GET_response # send the outputdata (GET response) back to the client # FillInStart # FillInEnd print("data sent") listener.close()