简体   繁体   中英

Web Server & Socket Programming

Currently doing an assignment in which we are programming sockets in python and thus creating a web server when the webserver.py code is executed.The code should then display HTTP headers and other information when you access a file(test.html)from the web server. Now my code works (or I'd like to believe so) and I have created a test.html file and the question goes on to say that I should place the test.html file in the same directory as the web server, where exactly is that on my local machine? I placed the test.html in the same folder as webserver.py in the python's root directory and proceeded to 127.0.0.1:1336/test.html to test my code but it doesn't work, where exactly on my machine is the webserver directory in which I should place test.html? Is it that I have to use wamp/xamp and place the test.html in there?

NB 1336 is the port I specified in the code to connect to.

  #import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 1336
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(('', serverPort)) #set up socket connection


serverSocket.listen(1) #tells the server to try a maximum of one connect request before ending connection



while True:
    #Establish the connection
    print 'Ready to serve...'


    connectionSocket, addr = serverSocket.accept()
    print 'connected to port',serverPort



try:


            message = connectionSocket.recv(1024) #Makes it so that you can recieve message from client


            filename = message.split()[1]
            f = open(filename[1:])

            outputdata = f.open(filename[1:])

            #Send one HTTP header line into socket2
            #Fill in start
            connectionSocket.send('HTTP/1.0 200 OK\r\n')



            #Send the content of the requested file to the client
            for i in range(0, len(outputdata)):
               connectionSocket.send(outputdata[i])
               connectionSocket.close()
except IOError:
    #Send response message for file not found

    print '404 Error : File Not Found.'

    #Close client socket

    connectionSocket.close()

serverSocket.close()

First of all, you shouldn't use the socket module to make a HTTP server. I recommend using the http.ser ver module, and change the working directory to where the html files are. Lets say i had test.html in C:\\User\\Desktop. An example:

from http.server import HTTPServer, CGIHTTPRequestHandler
import os
os.chdir("C:/User/Desktop")
address = ("", 1336)
httpserver = HTTPServer(address,, CGIHTTPRequestHandler)
httpserver.serve_forever()

Then you can access it by 127.0.0.1:1336/test.html

If this isn't the answer you are looking for, please add the webserver.py to the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM