python - how to send a command to a web server

  • Last Update :
  • Techknowledgy :

To perform your request, start with:

import requests

params = {
   "Image.I0.Appearance.Resolution": "800x450",
   "action": "update"
}

response = requests.get("http://192.168.0.9x/axis-cgi/param.cgi", params = params)

I think urllib2 is solution for your problem :)

import urllib2
content = urllib2.urlopen(some_url)

Look at requests. This is a third party library, but it's better than the built in urllib2.

import requests
r = requests.get(some_url)

Suggestion : 2

One class, HTTPServer, is a socketserver.TCPServer subclass. It creates and listens at the HTTP socket, dispatching the requests to a handler. Code to create and run the server looks like this:,The do_GET() and do_HEAD() functions are modified to run CGI scripts and serve the output, instead of serving files, if the request leads to somewhere below the cgi_directories path.,This class is identical to HTTPServer but uses threads to handle requests by using the ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on which HTTPServer would wait indefinitely.,The HTTPServer and ThreadingHTTPServer must be given a RequestHandlerClass on instantiation, of which this module provides three different variants:

def run(server_class = HTTPServer, handler_class = BaseHTTPRequestHandler):
   server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
   print("serving at port", PORT)
httpd.serve_forever()
python - m http.server
python - m http.server 9000
python - m http.server--bind 127.0 .0 .1
python - m http.server--directory / tmp /

Suggestion : 3

A webserver in Python can be setup in two ways. Python supports a webserver out of the box. You can start a web server with a one liner. ,The web server in this example can be accessed on your local network only. This can either be localhost or another network host. You could serve it cross location with a vpn.,Run the code below to start a custom web server. To create a custom web server, we need to use the HTTP protocol. ,That will open a webserver on port 8080. You can then open your browser at http://127.0.0.1:8080/

1
python3 - m http.server
1234567891011121314151617181920212223242526272829
# Python 3 server examplefrom http.server import BaseHTTPRequestHandler, HTTPServerimport timehostName = "localhost"serverPort = 8080class MyServer(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(bytes("<html>

<head>
   <title>https://pythonbasics.org</title>
</head>", "utf-8")) self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8")) self.wfile.write(bytes("

<body>", "utf-8")) self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8")) self.wfile.write(bytes("</body>

</html>", "utf-8"))if __name__ == "__main__": webServer = HTTPServer((hostName, serverPort), MyServer) print("Server started http://%s:%s" % (hostName, serverPort)) try: webServer.serve_forever() except KeyboardInterrupt: pass webServer.server_close() print("Server stopped.")