I'm using python3. I want to show a one page but I got 404 not found file:
import pymysql.cursors
import http.server
import socketserver
from furl import furl
class MyServer(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
print("jkjhjkhk") #printed
print(self.path)
if self.path == '/':
self.path = './desktop/formdailyactivities/index.html'
else:
self.send_response(200)
print (self.path)
f = furl(self.path)
# activity = f.args["activity"]
# time = f.args["time"]
# date = f.args["date"]
# print(activity)
# print(time)
# print(date)
# s1.insert(activity, time, date)
return http.server.SimpleHTTPRequestHandler.do_GET(self)
handler_object = MyServer
PORT = 8080
my_server = socketserver.TCPServer(("",PORT), handler_object)
my_server.serve_forever()
After testing your full code I can run it when I use
self.path = "index.html"orself.path = "folder/index.html"but only if"folder/index.html"is inside folder in which I run code.It can be for security reason. It may not read files which are below/outside folder in which I run code because someone could use path ie.
../../etc/passwdto steal passwords.As I know server
Apachealso restricts access to folders which are below/outside running folder.All server should restric it.
You may have to write own code which read data from file and send to browser.
You can get path to file with
http.serverto see source codemaybe it helps you.
In source code I saw function
copyfilewhich it uses to send file.Frankly, I would rathern use
Flaskto create it.EDIT:
After digging in source code I can see that original
do_GETusetranslate_path()to convertpathintocurrent_folder/pathwhich can be incorrect and it can't find file.Using code from source code I create this version - it runs almost all code from original
do_GETexcepttranslate_path()so I can use absolute path to display file.