Question: I need to do an assignment where I have the skeleton code for the Web server. I need to complete the skeleton code (I did

I need to do an assignment where I have the skeleton code for the Web server. I need to complete the

skeleton code (I did it and got a massage that socket created ,but after when, I tried it in the browser using url http://localhost:6789/helloworld.html , I got a massage:///This site cant be reached localhost refused to connect. Search Google for localhost 6789 hello world. ERR_CONNECTION_REFUSED/// what I did wrong?: I changed the code, I wrote html Hello world file and saved in the same folder as code file (maybe I need to do something else) , then I run code file got message: socket created )

Assignment: I need to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start

and #Fill in end. Each place may require one or more lines of code.

Running the Server

Put an HTML file (e.g., HelloWorld.html) in the same directory that the server is in. Run

the server program. Determine the IP address of the host that is running the server (e.g.,

128.238.251.26). From another host or the same host if you run the server on the local manchine, open a browser and provide the corresponding URL. For example: http://128.238.251.26:6789/HelloWorld.html

HelloWorld.html is the name of the file you placed in the server directory. Note also the use of the port number after the colon. You need to replace this port number with port number 6789. The browser should then display the contents of HelloWorld.html. If you omit ":6789", the browser will assume port 80 and you will get the web page from

the server only if your server is listening at port 80.

You can also run the server program on your own PC and access the webserver from the same PC using the IP address for local host (127.0.0.1). For example: http://127.0.0.1:6789/HelloWorld.html

Then try to get a file that is not present at the server. You should get a 404 Not Found

message.

ORIGINAL CODE:

#import socket module

import socket

import sys

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("Socket Created!!")

try:

#bind the socket

#fill in start

#fill in end

except socket.error as msg:

print("Bind failed. Error Code: " + str(msg[0]) + "Message: " + msg[1])

sys.exit()

print("Socket bind complete")

#start listening on the socket

#fill in start

#fill in end

print('Socket now listening')

while True:

#Establish the connection

connectionSocket, addr = #fill in start #fill in end

print('source address:' + str(addr))

try:

#Receive message from the socket

message = #fill in start #fill in end

print('message = ' + str(message))

#obtian the file name carried by the HTTP request message

filename = message.split()[1]

print('filename = ' + str(filename))

f = open(filename[1:], 'rb')

outputdata = f.read()

#Send the HTTP response header line to the socket

#fill in start

#fill in end

#Send the content of the requested file to the client

connectionSocket.send(outputdata)

#close the connectionSocket

#fill in start

#fill in end

print("Connection closed!")

except IOError:

#Send response message for file not found

#fill in start

#fill in end

#Close the client socket

#fill in start

#fill in end

serverSocket.close()

++++++++++++++++++

MY CODE:

#import socket module

import socket import sys

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("Socket Created!!") try: #bind the socket serversocket.bind((' ', 6789)) except socket.error as msg: print("Bind failed. Error Code: " + str(msg[0]) , "Message: " + msg[1]) sys.exit() print("Socket bind complete") #start listening on the socket serversocket.listen(10) print('Socket now listening') while True: #Establish the connection connectionSocket, addr = serversocket.accept() print('source address:' + str(addr)) try: #Receive message from the socket message = connectionSocket.recv(1024) print('message = ' + str(message)) #obtain the file name carried by the HTTP request message filename = message.split()[1] print('filename = ' + str(filename)) f = open(filename[1:], 'rb') outputdata = f.read() #Send the HTTP response header line to the socket headerLine = "HTTP/1.1 200 OK content-type:text/html charset=utf-8 " connectionSocket.send(headerLine.encode()) print (outputdata) #Send the content of the requested file to the client connectionSocket.send(outputdata) #close the connectionSocket connectionSocket.close() print("Connection closed!") except IOError: #Send response message for file not found headerLine="HTTP/1.1 404 Not Found content-type:text/html charset=utf-8 " connectionSocket.send(headerLine.encode()) connectionSocket.send("

Not Found

".encode()) #Close the client socket connectionSocket.close() serversocket.close()

++++++++++++++++++++

Helloworld.html

Basic Web Page Hello World!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!