{"id":4951,"date":"2021-01-11T17:05:14","date_gmt":"2021-01-11T09:05:14","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4951"},"modified":"2021-01-11T17:05:14","modified_gmt":"2021-01-11T09:05:14","slug":"python-pyro4","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4951","title":{"rendered":"Python Pyro4"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"operating\"><a href=\"https:\/\/blog.csdn.net\/xiaolewennofollow\/article\/details\/52155457\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"markdown_views\">\n<p>\u6700\u8fd1\u5728\u9879\u76ee\u4e2d\u7528\u5230\u4e00\u4e2apython RPC\u6846\u67b6 Pyro4, \u4e2d\u6587\u6587\u6863\u6bd4\u8f83\u5c11\uff0c\u5728\u8fd9\u91cc\u8bb0\u5f55\u4e00\u4e0b\u5173\u4e8e\u5b66\u4e60\u8fd9\u4e2a\u5e93\u7684\u7406\u89e3\u3002<\/p>\n<h2 id=\"rpc\u7684\u6982\u5ff5\"><a name=\"t0\"><\/a><a name=\"t0\"><\/a><strong>RPC\u7684\u6982\u5ff5<\/strong><\/h2>\n<p>\u9996\u5148\u4ec0\u4e48\u662fRPC\uff0cRPC\u662f\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u3002\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6709\u4e24\u53f0\u670d\u52a1\u5668A\uff0cB \u3002 \u4e00\u4e2a\u5e94\u7528\u90e8\u7f72\u5728A\u670d\u52a1\u5668\u4e0a\uff0c\u60f3\u8981\u8c03\u7528B\u670d\u52a1\u5668\u4e0a\u5e94\u7528\u63d0\u4f9b\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u3002\u7531\u4e8e\u4e0d\u5728\u4e00\u4e2a\u5185\u5b58\u7a7a\u95f4\u91cc\uff0c\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u3002\u9700\u8981\u7f51\u7edc\u6765\u4f20\u9012\u8c03\u7528\u7684\u8bed\u4e49\u548c\u4f20\u8fbe\u8c03\u7528\u7684\u6570\u636e\u3002<\/p>\n<p>\u57fa\u4e8e\u4e0a\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u629b\u51fa\u4e86\u4e00\u4e2aRPC\u6846\u67b6\u9700\u8981\u89e3\u51b3\u4ee5\u4e0b\u4e94\u70b9\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u7531\u4e8e\u5206\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u6211\u4eec\u9996\u5148\u8981\u89e3\u51b3\u7684\u662f\u901a\u8baf\u95ee\u9898\u3002\u4e3b\u8981\u662f\u901a\u8fc7\u5728\u5ba2\u6237\u673a\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acbTCP\u8fde\u63a5\u3002\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u4e2d\u4ea4\u6362\u7684\u6570\u636e\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u8fde\u63a5\u53bb\u4f20\u8f93\u3002\u8fde\u63a5\u53ef\u4ee5\u662f\u6309\u9700\u8fde\u63a5\u3002\u6bcf\u6b21\u8fdc\u7a0b\u8c03\u7528\u7ed3\u675f\u540e\u5173\u95ed\u3002\u4e5f\u53ef\u4ee5\u662f\u957f\u8fde\u63a5\uff0c\u591a\u6b21\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u5171\u4eab\u4e00\u4e2a\u8fde\u63a5\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u8981\u89e3\u51b3\u5bfb\u5740\u7684\u95ee\u9898\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cA\u670d\u52a1\u5668\u7684\u5e94\u7528\u8be5\u600e\u4e48\u6837\u53bb\u544a\u8bc9\u5e95\u5c42\u7684RPC\u6846\u67b6\u3002\u5982\u4f55\u8fde\u63a5\u5230B\u670d\u52a1\u5668\uff08\u4e3b\u673a\u540d\/IP\uff09\u4ee5\u53ca\u7279\u5b9a\u7684\u7aef\u53e3\u3002\u65b9\u6cd5\u7684\u540d\u79f0\u662f\u4ec0\u4e48\u3002\u8fd9\u6837\u624d\u80fd\u5b8c\u6210\u8c03\u7528\u3002\u6bd4\u5982\u6211\u4eec\u4e0b\u9762\u8981\u8bf4\u7684Pyro4 \uff0c\u5c31\u9700\u8981\u63d0\u4f9b\u4e00\u4e2aend point URI , \u6216\u8005\u662f\u4ecenameserver\u4e0a\u8fdb\u884c\u67e5\u627e\u3002<\/li>\n<li>\u5f53A\u670d\u52a1\u5668\u4e0a\u7684\u5e94\u7528\u53d1\u8d77\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u65f6\uff0c\u65b9\u6cd5\u7684\u53c2\u6570\u9700\u8981\u901a\u8fc7\u5e95\u5c42\u7684\u7f51\u7edc\u534f\u8bae\u5982TCP\u4f20\u9012\u5230B\u670d\u52a1\u5668\uff0c\u7531\u4e8e\u7f51\u7edc\u534f\u8bae\u662f\u57fa\u4e8e\u4e8c\u8fdb\u5236\u7684\u3002\u5185\u5b58\u4e2d\u7684\u53c2\u6570\u7684\u503c\u8981\u5e8f\u5217\u5316\u6210\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u3002\u4e5f\u5c31\u662fserialize\uff0c\u901a\u8fc7\u5bfb\u5740\u548c\u4f20\u8f93\u5c06\u5e8f\u5217\u5316\u7684\u4e8c\u8fdb\u5236\u53d1\u9001\u7ed9B\u670d\u52a1\u5668\u3002<\/li>\n<li>\u7b2c\u56db\uff0cB\u670d\u52a1\u5668\u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u9700\u8981\u5bf9\u53c2\u6570\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\uff08\u5e8f\u5217\u5316\u7684\u9006\u64cd\u4f5c\uff09\uff0c\u6062\u590d\u4e3a\u5185\u5b58\u4e2d\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u7136\u540e\u627e\u5230\u5bf9\u5e94\u7684\u65b9\u6cd5\uff08\u5bfb\u5740\u7684\u4e00\u90e8\u5206\uff09\u8fdb\u884c\u672c\u5730\u8c03\u7528\u3002\u7136\u540e\u5f97\u5230\u8fd4\u56de\u503c\u3002<\/li>\n<li>\u7b2c\u4e94\uff0c\u8fd4\u56de\u503c\u8fd8\u8981\u53d1\u9001\u56deA\u670d\u52a1\u5668\u4e0a\u7684\u5e94\u7528\uff0c\u4e5f\u8981\u7ecf\u8fc7\u5e8f\u5217\u5316\u7684\u65b9\u5f0f\u53d1\u9001\u3002\u670d\u52a1\u5668A\u63a5\u5230\u540e\uff0c\u518d\u53cd\u5e8f\u5217\u5316\uff0c\u6062\u590d\u4e3a\u5185\u5b58\u4e2d\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u4ea4\u7ed9A\u670d\u52a1\u5668\u4e0a\u7684\u5e94\u7528\u3002<\/li>\n<\/ol>\n<h2 id=\"pyro4\"><a name=\"t1\"><\/a><a name=\"t1\"><\/a><strong>Pyro4<\/strong><\/h2>\n<p>\u5173\u4e8e\u4e0a\u8ff0\u7684\u4e94\u70b9\uff0c\u5728Pyro4\u4e2d\u5df2\u7ecf\u8fdb\u884c\u4e86\u5b8c\u5584\u7684\u5c01\u88c5\u3002\u7528\u6237\u53ea\u9700\u8981\u4f7f\u7528Pyro4\u63d0\u4f9b\u7684API\u5c31\u53ef\u4ee5\u8f7b\u677e\u7684\u4f7f\u7528Python\u5b8c\u6210RPC\u8c03\u7528\u3002<br \/>\n\u4e0b\u9762\u4e3b\u8981\u8bb0\u5f55\u4e00\u4e0bPyro4\u4e2d\u7684\u4e3b\u8981\u6a21\u5757\uff08client, server,nameserver\uff0c\u4ee5\u53caPyro4\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177+<br \/>\n\uff09\u7684\u4f5c\u7528\u4ee5\u53ca\u4ee5\u53ca\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u9645\u7684\u5206\u5e03\u5f0f\u73af\u5883\u7684\u5b9e\u4f8b\uff08\u76f8\u5bf9\u4e8e\u5b98\u65b9\u6587\u6863\u4e2d\u7ed9\u51fa\u7684\u90fd\u5728localHost\u7684\u5355\u673a\u4f2a\u5206\u5e03\u5f0f\u793a\u4f8b\uff09\u3002<br \/>\n\u5b9e\u9645\u4e0aPyro4\u63d0\u4f9b\u4e86\u5f88\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5982\u679c\u9700\u8981\u8be6\u7ec6\u4e86\u89e3\u5176\u529f\u80fd\uff0c\u53ef\u4ee5\u8bbf\u95ee<br \/>\n<a href=\"https:\/\/pythonhosted.org\/Pyro4\/intro.html#about-pyro-feature-overview\">\u5b98\u65b9\u6587\u6863<\/a><\/p>\n<p>\u6211\u4eec\u5148\u4ee5\u4e00\u4e2a\u672c\u5730\u7684\u666e\u901a\u8c03\u7528\u4e3a\u4f8b\uff0c\u7136\u540e\u5c06\u5176\u6539\u5199\u4e3aRPC\u8c03\u7528<\/p>\n<h3 id=\"warehousepy\"><a name=\"t2\"><\/a><a name=\"t2\"><\/a>warehouse.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs python has-numbering\"><span class=\"hljs-keyword\">from<\/span> __future__ <span class=\"hljs-keyword\">import<\/span> print_function\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Warehouse<\/span><span class=\"hljs-params\">(object)<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        self.contents = [<span class=\"hljs-string\">\"chair\"<\/span>, <span class=\"hljs-string\">\"bike\"<\/span>, <span class=\"hljs-string\">\"flashlight\"<\/span>, <span class=\"hljs-string\">\"laptop\"<\/span>, <span class=\"hljs-string\">\"couch\"<\/span>]\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">list_contents<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n        <span class=\"hljs-keyword\">return<\/span> self.contents\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">take<\/span><span class=\"hljs-params\">(self, name, item)<\/span>:<\/span>\r\n        self.contents.remove(item)\r\n        print(<span class=\"hljs-string\">\"{0} took the {1}.\"<\/span>.format(name, item))\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">store<\/span><span class=\"hljs-params\">(self, name, item)<\/span>:<\/span>\r\n        self.contents.append(item)\r\n        print(<span class=\"hljs-string\">\"{0} stored the {1}.\"<\/span>.format(name, item))<\/code><\/pre>\n<h3 id=\"personpy\"><a name=\"t3\"><\/a><a name=\"t3\"><\/a>person.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs python has-numbering\"><span class=\"hljs-keyword\">from<\/span> __future__ <span class=\"hljs-keyword\">import<\/span> print_function\r\n<span class=\"hljs-keyword\">import<\/span> sys\r\n\r\n<span class=\"hljs-keyword\">if<\/span> sys.version_info &lt; (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">0<\/span>):\r\n    input = raw_input\r\n\r\n\r\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span><span class=\"hljs-params\">(object)<\/span>:<\/span>\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\r\n        self.name = name\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">visit<\/span><span class=\"hljs-params\">(self, warehouse)<\/span>:<\/span>\r\n        print(<span class=\"hljs-string\">\"This is {0}.\"<\/span>.format(self.name))\r\n        self.deposit(warehouse)\r\n        self.retrieve(warehouse)\r\n        print(<span class=\"hljs-string\">\"Thank you, come again!\"<\/span>)\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">deposit<\/span><span class=\"hljs-params\">(self, warehouse)<\/span>:<\/span>\r\n        print(<span class=\"hljs-string\">\"The warehouse contains:\"<\/span>, warehouse.list_contents())\r\n        item = input(<span class=\"hljs-string\">\"Type a thing you want to store (or empty): \"<\/span>).strip()\r\n        <span class=\"hljs-keyword\">if<\/span> item:\r\n            warehouse.store(self.name, item)\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">retrieve<\/span><span class=\"hljs-params\">(self, warehouse)<\/span>:<\/span>\r\n        print(<span class=\"hljs-string\">\"The warehouse contains:\"<\/span>, warehouse.list_contents())\r\n        item = input(<span class=\"hljs-string\">\"Type something you want to take (or empty): \"<\/span>).strip()\r\n        <span class=\"hljs-keyword\">if<\/span> item:\r\n            warehouse.take(self.name, item)<\/code><\/pre>\n<h3 id=\"visitpy\"><a name=\"t4\"><\/a><a name=\"t4\"><\/a>visit.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs python has-numbering\"><span class=\"hljs-comment\"># This is the code that runs this example.<\/span>\r\n<span class=\"hljs-keyword\">from<\/span> warehouse <span class=\"hljs-keyword\">import<\/span> Warehouse\r\n<span class=\"hljs-keyword\">from<\/span> person <span class=\"hljs-keyword\">import<\/span> Person\r\n\r\nwarehouse = Warehouse()\r\njanet = Person(<span class=\"hljs-string\">\"Janet\"<\/span>)\r\nhenry = Person(<span class=\"hljs-string\">\"Henry\"<\/span>)\r\njanet.visit(warehouse)\r\nhenry.visit(warehouse)<\/code><\/pre>\n<p>\u6267\u884c\u6548\u679c\u5982\u4e0b<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs livecodeserver has-numbering\">$ python visit.py\r\nThis is Janet.\r\nThe warehouse <span class=\"hljs-operator\">contains<\/span>: [<span class=\"hljs-string\">'chair'<\/span>, <span class=\"hljs-string\">'bike'<\/span>, <span class=\"hljs-string\">'flashlight'<\/span>, <span class=\"hljs-string\">'laptop'<\/span>, <span class=\"hljs-string\">'couch'<\/span>]\r\nType <span class=\"hljs-operator\">a<\/span> thing you want <span class=\"hljs-built_in\">to<\/span> store (<span class=\"hljs-operator\">or<\/span> <span class=\"hljs-constant\">empty<\/span>): television   <span class=\"hljs-comment\"># typed in<\/span>\r\nJanet stored <span class=\"hljs-operator\">the<\/span> television.\r\nThe warehouse <span class=\"hljs-operator\">contains<\/span>: [<span class=\"hljs-string\">'chair'<\/span>, <span class=\"hljs-string\">'bike'<\/span>, <span class=\"hljs-string\">'flashlight'<\/span>, <span class=\"hljs-string\">'laptop'<\/span>, <span class=\"hljs-string\">'couch'<\/span>, <span class=\"hljs-string\">'television'<\/span>]\r\nType something you want <span class=\"hljs-built_in\">to<\/span> take (<span class=\"hljs-operator\">or<\/span> <span class=\"hljs-constant\">empty<\/span>): couch    <span class=\"hljs-comment\"># &lt;-- typed in<\/span>\r\nJanet took <span class=\"hljs-operator\">the<\/span> couch.\r\nThank you, come again!\r\nThis is Henry.\r\nThe warehouse <span class=\"hljs-operator\">contains<\/span>: [<span class=\"hljs-string\">'chair'<\/span>, <span class=\"hljs-string\">'bike'<\/span>, <span class=\"hljs-string\">'flashlight'<\/span>, <span class=\"hljs-string\">'laptop'<\/span>, <span class=\"hljs-string\">'television'<\/span>]\r\nType <span class=\"hljs-operator\">a<\/span> thing you want <span class=\"hljs-built_in\">to<\/span> store (<span class=\"hljs-operator\">or<\/span> <span class=\"hljs-constant\">empty<\/span>): bricks   <span class=\"hljs-comment\"># &lt;-- typed in<\/span>\r\nHenry stored <span class=\"hljs-operator\">the<\/span> bricks.\r\nThe warehouse <span class=\"hljs-operator\">contains<\/span>: [<span class=\"hljs-string\">'chair'<\/span>, <span class=\"hljs-string\">'bike'<\/span>, <span class=\"hljs-string\">'flashlight'<\/span>, <span class=\"hljs-string\">'laptop'<\/span>, <span class=\"hljs-string\">'television'<\/span>, <span class=\"hljs-string\">'bricks'<\/span>]\r\nType something you want <span class=\"hljs-built_in\">to<\/span> take (<span class=\"hljs-operator\">or<\/span> <span class=\"hljs-constant\">empty<\/span>): bike   <span class=\"hljs-comment\"># &lt;-- typed in<\/span>\r\nHenry took <span class=\"hljs-operator\">the<\/span> bike.\r\nThank you, come again!<\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u5176\u6539\u5199\u4e3a\u5206\u5e03\u5f0f\u7684RPC\u8c03\u7528\uff0c\u6211\u8fd9\u91cc\u6709\u4e09\u53f0host, ip\u5206\u522b\u662f<\/p>\n<ul>\n<li>host1:10.133.1.224<\/li>\n<li>host2:10.133.1.220<\/li>\n<li>host3:10.133.1.221<\/li>\n<\/ul>\n<p>\u5176\u4e2dhost1 \u6267\u884cvisit.py\uff08\u540c\u65f6person.py\u4e5f\u5728\u8fd9\u53f0\u673a\u5668\u4e0a\uff09\uff0c\u62c5\u4efbRPC \u4e2dclient\u7684\u89d2\u8272\u3002<br \/>\nhost2 \u6267\u884cwarehouse.py, \u62c5\u4efbRPC\u4e2dserver\u7684\u89d2\u8272\u3002<br \/>\nhost3 \u6267\u884cnameserver.py,\u62c5\u4efbnameserver\u7684\u89d2\u8272\u3002\u8fd9\u91cc\u8981\u7279\u5730\u8bf4\u4e00\u4e0bnameserver\u7684\u4f5c\u7528\uff1a\u63d0\u4f9b\u8fdc\u7a0b\u8c03\u7528\u5bf9\u8c61\u7684\u903b\u8f91\u540d\u4e0eURI\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u4f7f\u5f97client \u5728\u8fdb\u884c\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u65f6\u53ea\u9700\u8981\u6307\u5b9a\u8c03\u7528\u7684\u8fdc\u7a0b\u5bf9\u8c61\u7684\u903b\u8f91\u540d\u79f0(\u800c\u4e0d\u7528\u590d\u6742\u7684URI )\u548c\u5bf9\u5e94\u7684nameserver\u5c31\u53ef\u4ee5\u8f7b\u677e\u7684\u5b8c\u6210\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u3002<\/p>\n<p>\u6539\u4e3aRPC\u8c03\u7528\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<h3 id=\"visitpy-1\"><a name=\"t5\"><\/a><a name=\"t5\"><\/a>visit.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs avrasm has-numbering\"> <span class=\"hljs-preprocessor\"># This is the code that visits the warehouse.<\/span>\r\n import sys\r\n import Pyro4\r\n from person import Person\r\n sys<span class=\"hljs-preprocessor\">.excepthook<\/span> = Pyro4<span class=\"hljs-preprocessor\">.util<\/span><span class=\"hljs-preprocessor\">.excepthook<\/span> \r\n warehouse=Pyro4<span class=\"hljs-preprocessor\">.Proxy<\/span>(<span class=\"hljs-string\">\"PYRONAME:example.warehouse\"<\/span>)\r\n janet = Person(<span class=\"hljs-string\">\"Janet\"<\/span>)\r\n henry = Person(<span class=\"hljs-string\">\"Henry\"<\/span>)\r\n janet<span class=\"hljs-preprocessor\">.visit<\/span>(warehouse)\r\n henry<span class=\"hljs-preprocessor\">.visit<\/span>(warehouse)\r\n<\/code><\/pre>\n<p>person.py \u65e0\u6539\u52a8\uff0c\u540c\u4e0a<\/p>\n<h3 id=\"warehousepy-1\"><a name=\"t6\"><\/a><a name=\"t6\"><\/a>warehouse.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs python has-numbering\"><span class=\"hljs-keyword\">from<\/span> __future__ <span class=\"hljs-keyword\">import<\/span> print_function\r\n <span class=\"hljs-keyword\">import<\/span> Pyro4\r\n <span class=\"hljs-comment\">#import person<\/span>\r\n\r\n\r\n <span class=\"hljs-decorator\">@Pyro4.expose<\/span>\r\n <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Warehouse<\/span><span class=\"hljs-params\">(object)<\/span>:<\/span>\r\n     <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n         self.contents = [<span class=\"hljs-string\">\"chair\"<\/span>, <span class=\"hljs-string\">\"bike\"<\/span>, <span class=\"hljs-string\">\"flashlight\"<\/span>, <span class=\"hljs-string\">\"laptop\"<\/span>, <span class=\"hljs-string\">\"couch\"<\/span>]\r\n\r\n     <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">list_contents<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\r\n         <span class=\"hljs-keyword\">return<\/span> self.contents\r\n\r\n     <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">take<\/span><span class=\"hljs-params\">(self, name, item)<\/span>:<\/span>\r\n         self.contents.remove(item)\r\n         print(<span class=\"hljs-string\">\"{0} took the {1}.\"<\/span>.format(name, item))\r\n\r\n     <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">store<\/span><span class=\"hljs-params\">(self, name, item)<\/span>:<\/span>\r\n         self.contents.append(item)\r\n         print(<span class=\"hljs-string\">\"{0} stored the {1}.\"<\/span>.format(name, item))\r\n\r\n\r\n <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\r\n     Pyro4.naming.locateNS(host=<span class=\"hljs-string\">'10.133.1.221'<\/span>)\r\n     Pyro4.reDaemon.serveSimple(\r\n              {\r\n                  Warehouse: <span class=\"hljs-string\">\"example.warehouse\"<\/span>\r\n              },\r\n              ns = <span class=\"hljs-keyword\">True<\/span>,\r\n              host = <span class=\"hljs-string\">'10.133.1.220'<\/span>\r\n              )\r\n\r\n\r\n <span class=\"hljs-keyword\">if<\/span> __name__==<span class=\"hljs-string\">\"__main__\"<\/span>:\r\n     main()\r\n ~              <\/code><\/pre>\n<h3 id=\"nameserverpy\"><a name=\"t7\"><\/a><a name=\"t7\"><\/a>nameserver.py<\/h3>\n<pre class=\"prettyprint\"><code class=\"hljs java has-numbering\"><span class=\"hljs-keyword\">import<\/span> Pyro4\r\n\r\n Pyro4.naming.startNSloop(host=<span class=\"hljs-string\">'10.133.1.221'<\/span>)\r\n<\/code><\/pre>\n<p>\u9996\u5148\u6267\u884cnameserver.py,<br \/>\n\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u5728\u6267\u884cnameserver.py\u7684\u8fd9\u53f0\u673a\u5668\u4e0a\u67e5\u770b<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs ruby has-numbering\">wxl<span class=\"hljs-variable\">@dev<\/span><span class=\"hljs-symbol\">:~<\/span><span class=\"hljs-variable\">$ <\/span>pyro4-nsc ping\r\n<span class=\"hljs-constant\">Name<\/span> server ping ok.\r\n<\/code><\/pre>\n<p>\u53d1\u73b0\u6b64\u65f6nameserver\u5df2\u7ecf\u6210\u529f\u8fd0\u884c\u3002<\/p>\n<p>\u518d\u6267\u884c<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs ruby has-numbering\">wxl<span class=\"hljs-variable\">@dev<\/span><span class=\"hljs-symbol\">:~<\/span><span class=\"hljs-variable\">$ <\/span>pyro4-nsc list\r\n--------<span class=\"hljs-constant\">START<\/span> <span class=\"hljs-constant\">LIST<\/span> \r\n<span class=\"hljs-constant\">Pyro<\/span>.<span class=\"hljs-constant\">NameServer<\/span> --&gt; <span class=\"hljs-constant\">PYRO<\/span><span class=\"hljs-symbol\">:Pyro<\/span>.<span class=\"hljs-constant\">NameServer<\/span><span class=\"hljs-variable\">@10<\/span>.<span class=\"hljs-number\">133.1<\/span>.<span class=\"hljs-number\">221<\/span><span class=\"hljs-symbol\">:<\/span><span class=\"hljs-number\">9090<\/span>\r\n    <span class=\"hljs-symbol\">metadata:<\/span> [u<span class=\"hljs-string\">'class:Pyro4.naming.NameServer'<\/span>]\r\n--------<span class=\"hljs-constant\">END<\/span> <span class=\"hljs-constant\">LIST<\/span> \r\n<\/code><\/pre>\n<p>\u53d1\u73b0\u6b64\u65f6nameserver\u4e0a\u6ca1\u6709\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u5bf9\u8c61\u3002<\/p>\n<p>\u5728host2\u4e0a\u542f\u52a8warehouse.py<\/p>\n<p>\u518d\u6b21\u5728nameserver\u4e0a\u67e5\u770b\u53d1\u73b0\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs ruby has-numbering\">wxl<span class=\"hljs-variable\">@dev<\/span><span class=\"hljs-symbol\">:~<\/span><span class=\"hljs-variable\">$ <\/span>pyro4-nsc list\r\n--------<span class=\"hljs-constant\">START<\/span> <span class=\"hljs-constant\">LIST<\/span> \r\n<span class=\"hljs-constant\">Pyro<\/span>.<span class=\"hljs-constant\">NameServer<\/span> --&gt; <span class=\"hljs-constant\">PYRO<\/span><span class=\"hljs-symbol\">:Pyro<\/span>.<span class=\"hljs-constant\">NameServer<\/span><span class=\"hljs-variable\">@10<\/span>.<span class=\"hljs-number\">133.1<\/span>.<span class=\"hljs-number\">221<\/span><span class=\"hljs-symbol\">:<\/span><span class=\"hljs-number\">9090<\/span>\r\n    <span class=\"hljs-symbol\">metadata:<\/span> [u<span class=\"hljs-string\">'class:Pyro4.naming.NameServer'<\/span>]\r\nexample.warehouse --&gt; <span class=\"hljs-constant\">PYRO<\/span><span class=\"hljs-symbol\">:obj_cc4b9c4bf9a148ca9cf2f1445e578a75<\/span><span class=\"hljs-variable\">@10<\/span>.<span class=\"hljs-number\">133.1<\/span>.<span class=\"hljs-number\">220<\/span><span class=\"hljs-symbol\">:<\/span><span class=\"hljs-number\">36178<\/span>\r\n--------<span class=\"hljs-constant\">END<\/span> <span class=\"hljs-constant\">LIST<\/span> \r\n<\/code><\/pre>\n<p>\u5df2\u7ecf\u6ce8\u518c\u5230\u8fd9\u53f0nameserver\u4e0a\u4e86\u3002<\/p>\n<p>\u6b64\u65f6host1\u6267\u884cvisit.py\uff0c\u53d1\u73b0\u53ef\u4ee5\u8c03\u7528\u8fdc\u7a0bwarehouse\u5bf9\u8c61<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u6700\u8fd1\u5728\u9879\u76ee\u4e2d\u7528\u5230\u4e00\u4e2apytho<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-4951","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4951"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4951\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}