harrygriet 0 Unverified User

Hi I am trying to create a simple webserver program in python 3.2.2 and I want the server to process different methods other than GET and POST. These methods are handled by default by the library.

I want to handle a OPTIONS method and I used a do_OPTIONS() method to handle it. But the programs still doesnt recognize this method. Can someone please help me with that.

import string,cgi,time
from os import curdir, sep
from http.server import BaseHTTPRequestHandler, HTTPServer
#import pri

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            if self.path.endswith(".html"):
                f = open(curdir + sep + self.path) #self.path has /test.html
#note that this potentially makes every file on your computer readable by the internet

                self.send_response(200)
                self.send_header('Content-type',   'text/html')
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
                return
            if self.path.endswith(".esp"):   #our dynamic content
                self.send_response(200)
                self.send_header('Content-type',   'text/html')
                self.end_headers()
                self.wfile.write("hey, today is the" + str(time.localtime()[7]))
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                query=cgi.parse_multipart(self.rfile, pdict)
            self.send_response(301)
            
            self.end_headers()
            upfilecontent = query.get('upfile')
            print (("filecontent", upfilecontent[0]))
            self.wfile.write("<HTML>POST OK.<BR><BR>");
            self.wfile.write(upfilecontent[0]);
            
        except :
            pass
         
def do_OPTIONS(self):
         try:
            self.send_response(200)
         except:
            pass

def main():
    try:
        server = HTTPServer(('', 5080), MyHandler)
        print ('started httpserver...')
        server.serve_forever()
    except KeyboardInterrupt:
        print ('^C received, shutting down server')
        server.socket.close()

if __name__ == '__main__':
    main()

and I get the below error:

"http://www.w3.org/T
tml4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="t
/html;charset=utf-8">
<title>Error response</title>
</head>
<bod
<h1>Error response</h1>
<p>Error code: 400</p>
<p>Messag
Bad HTTP/0.9 request type ('OPTIONS').</p>
<p>Error code explanation:
- Bad request syntax or unsupported method.</p>
</body>
</html>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.