店播爬取Python脚本

service.py 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # https://developers.google.com/protocol-buffers/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """DEPRECATED: Declares the RPC service interfaces.
  31. This module declares the abstract interfaces underlying proto2 RPC
  32. services. These are intended to be independent of any particular RPC
  33. implementation, so that proto2 services can be used on top of a variety
  34. of implementations. Starting with version 2.3.0, RPC implementations should
  35. not try to build on these, but should instead provide code generator plugins
  36. which generate code specific to the particular RPC implementation. This way
  37. the generated code can be more appropriate for the implementation in use
  38. and can avoid unnecessary layers of indirection.
  39. """
  40. __author__ = 'petar@google.com (Petar Petrov)'
  41. class RpcException(Exception):
  42. """Exception raised on failed blocking RPC method call."""
  43. pass
  44. class Service(object):
  45. """Abstract base interface for protocol-buffer-based RPC services.
  46. Services themselves are abstract classes (implemented either by servers or as
  47. stubs), but they subclass this base interface. The methods of this
  48. interface can be used to call the methods of the service without knowing
  49. its exact type at compile time (analogous to the Message interface).
  50. """
  51. def GetDescriptor():
  52. """Retrieves this service's descriptor."""
  53. raise NotImplementedError
  54. def CallMethod(self, method_descriptor, rpc_controller,
  55. request, done):
  56. """Calls a method of the service specified by method_descriptor.
  57. If "done" is None then the call is blocking and the response
  58. message will be returned directly. Otherwise the call is asynchronous
  59. and "done" will later be called with the response value.
  60. In the blocking case, RpcException will be raised on error.
  61. Preconditions:
  62. * method_descriptor.service == GetDescriptor
  63. * request is of the exact same classes as returned by
  64. GetRequestClass(method).
  65. * After the call has started, the request must not be modified.
  66. * "rpc_controller" is of the correct type for the RPC implementation being
  67. used by this Service. For stubs, the "correct type" depends on the
  68. RpcChannel which the stub is using.
  69. Postconditions:
  70. * "done" will be called when the method is complete. This may be
  71. before CallMethod() returns or it may be at some point in the future.
  72. * If the RPC failed, the response value passed to "done" will be None.
  73. Further details about the failure can be found by querying the
  74. RpcController.
  75. """
  76. raise NotImplementedError
  77. def GetRequestClass(self, method_descriptor):
  78. """Returns the class of the request message for the specified method.
  79. CallMethod() requires that the request is of a particular subclass of
  80. Message. GetRequestClass() gets the default instance of this required
  81. type.
  82. Example:
  83. method = service.GetDescriptor().FindMethodByName("Foo")
  84. request = stub.GetRequestClass(method)()
  85. request.ParseFromString(input)
  86. service.CallMethod(method, request, callback)
  87. """
  88. raise NotImplementedError
  89. def GetResponseClass(self, method_descriptor):
  90. """Returns the class of the response message for the specified method.
  91. This method isn't really needed, as the RpcChannel's CallMethod constructs
  92. the response protocol message. It's provided anyway in case it is useful
  93. for the caller to know the response type in advance.
  94. """
  95. raise NotImplementedError
  96. class RpcController(object):
  97. """An RpcController mediates a single method call.
  98. The primary purpose of the controller is to provide a way to manipulate
  99. settings specific to the RPC implementation and to find out about RPC-level
  100. errors. The methods provided by the RpcController interface are intended
  101. to be a "least common denominator" set of features which we expect all
  102. implementations to support. Specific implementations may provide more
  103. advanced features (e.g. deadline propagation).
  104. """
  105. # Client-side methods below
  106. def Reset(self):
  107. """Resets the RpcController to its initial state.
  108. After the RpcController has been reset, it may be reused in
  109. a new call. Must not be called while an RPC is in progress.
  110. """
  111. raise NotImplementedError
  112. def Failed(self):
  113. """Returns true if the call failed.
  114. After a call has finished, returns true if the call failed. The possible
  115. reasons for failure depend on the RPC implementation. Failed() must not
  116. be called before a call has finished. If Failed() returns true, the
  117. contents of the response message are undefined.
  118. """
  119. raise NotImplementedError
  120. def ErrorText(self):
  121. """If Failed is true, returns a human-readable description of the error."""
  122. raise NotImplementedError
  123. def StartCancel(self):
  124. """Initiate cancellation.
  125. Advises the RPC system that the caller desires that the RPC call be
  126. canceled. The RPC system may cancel it immediately, may wait awhile and
  127. then cancel it, or may not even cancel the call at all. If the call is
  128. canceled, the "done" callback will still be called and the RpcController
  129. will indicate that the call failed at that time.
  130. """
  131. raise NotImplementedError
  132. # Server-side methods below
  133. def SetFailed(self, reason):
  134. """Sets a failure reason.
  135. Causes Failed() to return true on the client side. "reason" will be
  136. incorporated into the message returned by ErrorText(). If you find
  137. you need to return machine-readable information about failures, you
  138. should incorporate it into your response protocol buffer and should
  139. NOT call SetFailed().
  140. """
  141. raise NotImplementedError
  142. def IsCanceled(self):
  143. """Checks if the client cancelled the RPC.
  144. If true, indicates that the client canceled the RPC, so the server may
  145. as well give up on replying to it. The server should still call the
  146. final "done" callback.
  147. """
  148. raise NotImplementedError
  149. def NotifyOnCancel(self, callback):
  150. """Sets a callback to invoke on cancel.
  151. Asks that the given callback be called when the RPC is canceled. The
  152. callback will always be called exactly once. If the RPC completes without
  153. being canceled, the callback will be called after completion. If the RPC
  154. has already been canceled when NotifyOnCancel() is called, the callback
  155. will be called immediately.
  156. NotifyOnCancel() must be called no more than once per request.
  157. """
  158. raise NotImplementedError
  159. class RpcChannel(object):
  160. """Abstract interface for an RPC channel.
  161. An RpcChannel represents a communication line to a service which can be used
  162. to call that service's methods. The service may be running on another
  163. machine. Normally, you should not use an RpcChannel directly, but instead
  164. construct a stub {@link Service} wrapping it. Example:
  165. Example:
  166. RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234")
  167. RpcController controller = rpcImpl.Controller()
  168. MyService service = MyService_Stub(channel)
  169. service.MyMethod(controller, request, callback)
  170. """
  171. def CallMethod(self, method_descriptor, rpc_controller,
  172. request, response_class, done):
  173. """Calls the method identified by the descriptor.
  174. Call the given method of the remote service. The signature of this
  175. procedure looks the same as Service.CallMethod(), but the requirements
  176. are less strict in one important way: the request object doesn't have to
  177. be of any specific class as long as its descriptor is method.input_type.
  178. """
  179. raise NotImplementedError