Python respond to HTTP Request

前端 未结 2 1462
走了就别回头了
走了就别回头了 2020-12-25 08:14

I am trying to do something that I assume is very simple, but since I am fairly new to Python I haven\'t been able to find out how. I need to execute a function in my Python

2条回答
  •  梦毁少年i
    2020-12-25 08:52

    This is indeed very simple to do in python:

    import SocketServer
    from BaseHTTPServer import BaseHTTPRequestHandler
    
    def some_function():
        print "some_function got called"
    
    class MyHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            if self.path == '/captureImage':
                # Insert your code here
                some_function()
    
            self.send_response(200)
    
    httpd = SocketServer.TCPServer(("", 8080), MyHandler)
    httpd.serve_forever()
    

    Depending on where you want to go from here, you might want to checkout the documentation for BaseHttpServer, or look into a more full featured web framework like Django.

提交回复
热议问题