Enabling Web Services
1. Basic approach
Python comes with a simple server program that makes it easier to open services.
The original SimpleHTTPServer command has been changed in python3 to use the following:
1. cd www directory
2. python -m
If it is successfully opened, the output will be "Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...", indicating that the service is opened on port 8000 of the local machine.
If you need to run in the background, you can add the "&" symbol after the command, and Ctrl+C will not close the service, as follows:
python -m &
If you want to keep the service, prefix the command with nohup to ignore all hangup signals, as follows:
nohup python -m 8001
2. Specify the port
If the default port is not used, it can be turned on with a port parameter such as:
python -m 8001
Then the http service will be opened on port 8001.
Using Web Services
You can use http://0.0.0.0:8000/ferret outwwwWeb files in the directory, if not it will show the files in the directory.
You can also use the ifconfig command to view the local IP and use it.
Addendum: python create http service
contexts
Invalid memory access often occurs when calling dlls with java, use java-Python-dll instead.
Python provides functionality to java via http services.
matrix
Python3.7
to process the request and return a response.
Print Log
filename is the name of the input log, the default is the same directory, without the file will create a new
filemode a is the append mode, w is the overwrite mode
import logging ( level=, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename="", filemode='a' ) ("xxxx")
Calling the dll
pchar - ctypes.c_char_p
integer used bytes(0), byref(ctypes.c_void_p(0)) are OK, did not go more in-depth study, if there is any error, please correct me.
import ctypes from ctypes import * dll = ('C:\\xxx\\') print("The dll version number is: "+ str(()) ) name = ctypes.c_char_p(b"gc") roomno = ctypes.c_char_p(bytes(("utf-8"))) begintime = ctypes.c_char_p(bytes(("utf-8"))) endtime = ctypes.c_char_p(bytes(("utf-8"))) cardno = ctypes.c_void_p(0) ...
http Program I
Be aware that you must have response = response_start_line + response_headers + "\r\n" + response_body
Splicing the answer message is required to return the correct answer to the browser.
# coding:utf-8 import socket from multiprocessing import Process def handle_client(client_socket): # Get client request data request_data = client_socket.recv(1024) print("request:", request_data) # Construct response data response_start_line = "HTTP/1.1 200 OK\r\n" response_headers = "Server: My server\r\n" response_body = "helloWorld!" response = response_start_line + response_headers + "\r\n" + response_body print("response:", response) # Return response data to the client client_socket.send(bytes(response, "utf-8")) # Close the client connection client_socket.close() if __name__ == "__main__": server_socket = (socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("", 8888)) server_socket.listen(120) print("success") while True: client_socket, client_address = server_socket.accept() print("[%s, %s] user is connected." % client_address) handle_client_process = Process(target=handle_client, args=(client_socket,)) handle_client_process.start() client_socket.close()
Full Code
Another http method
#. coding:utf-8 . from import HTTPServer import ctypes from ctypes import * # HTTPRequestHandler class import import socketserver import logging # pyinstaller -F class testHTTPServer_RequestHandler(): # GET def do_GET(self): ('start make ') str2 = str() print("revice: " + str2) if "xxx" in str2: # todo your specific business operations if "xxx" in str2: print("hahaha") ('hahaha') # response_body = "0" self.send_response(200) # Send headers self.send_header('Content-type','text/html') self.end_headers() # Send message back to client message = "Hello world!" # Write content as utf-8 data (bytes(message, "utf8")) return else: print("1else") self.send_response(200) # Send headers self.send_header('Content-type', 'text/html') self.end_headers() # Send message back to client message = "Hello world222333!" # Write content as utf-8 data (bytes(message, "utf8")) return def run(): print('starting server...') ( level=, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename="http_make_card.txt", filemode='a+' ) # Server settings server_address = ('127.0.0.1', 8888) httpd = HTTPServer(server_address, testHTTPServer_RequestHandler) print('running server...') httpd.serve_forever() run()
Packaging exe
pip install pyinstaller
pyinstaller -F will do the trick, and the current directory will generate the
defraud
1、No module named ‘'; ‘http' is not a package
At that time, I built a py called http myself, and deleted it as normal
2、UnicodeDecodeError: ‘utf-8' codec can't decode byte 0xce in position 130: invalid continuat
Just save as utf-8
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.