In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to quickly establish web services in python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. Description
(1) sometimes we need a simple and quick way to build RPC services. Just let program B call program A.
(2) you don't need to know any technology about this, but we only need something so simple. We can use a protocol (the corresponding Python library implementation SimpleXMLRPCServer) to handle this kind of thing.
2. Instance from SimpleXMLRPCServer import SimpleXMLRPCServer def file_reader (file_name): with open (file_name,'r') as f: return f.read () server = SimpleXMLRPCServer (('localhost', 8000)) server.register_introspection_functions () server.register_function (file_reader) server.serve_forever ()
Instance expansion:
Python implements a simple web server
Import reimport socket def service_cilent (new_socket): request = new_socket.recv (1024). Decode ("utf-8") # Python splitlines () separated by line (',',') returns a list of lines as elements, if the parameter keepends is False and does not contain newline characters, if True, the newline character is retained. Request_lines = request.splitlines () print (request_lines) file_name = "" ret = re.match (r "[^ /] + (/ [^] *)", request_lines [0]) if ret: file_name = ret.group (1) if file_name = = "/": file_name = "index.html" try: F = open (file_name "rb") except: response = "HTTP/1.1 404 NOT FOUND" response + = "- file not found-" new_socket.send (response.encode ("utf-8")) else: # Open the file successfully read the file and then close the file pointer html_content = f.read () f.close () # data to be sent to the browser -header response = "HTTP/1.1 200 OK" # send response header to browser new_socket.send (response.encode ("utf-8")) # send response body to browser new_socket.send (html_content) # close socket new_socket.close () def main (): # create socket tcp_server_socket = socket.socket (socket.AF_INET Socket.SOCK_STREAM) # tcp_server_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # bind tcp_server_socket.bind ((", 7089)) # listening socket tcp_server_socket.listen (128C) while True: new_socket Cilent_addr = tcp_server_socket.accept () service_cilent (new_socket) # turn off the listening socket tcp_server_socket.close () if _ _ name__ ='_ _ main__': main () these are all the contents of the article "how to quickly set up web Services in python" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.