I am attempting to build a web server using python to host a website. But there is a flaw in my code which I cannot seem to find an answer to. That flaw is opening files in the same directory of the web server script. Here is the code:
import socket
host, port = '', 80
website = '''
<a href='README.txt'>Read me file</a>
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print('SERVER STARTED ON PORT {}'.format(port))
s.listen(1)
while True:
clientconnection, clientaddress = s.accept()
r = clientconnection.recv(1024)
print(r)
response = """
HTTPS/1.1 200 OK
{}
""".format(website)
clientconnection.sendall(bytes(response.encode()))
clientconnection.close()
The links are working to other sites. Any Ideas? Thx in advance
There is something called SimpleHTTPServer
. As Rafael Barros said. You should also then create a file titled: index.html
. If you don't have an index.html file in the directory, then all files and directories will be listed.
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.