From 53a51d101be07e2073212fa5880ac32be1f6b697 Mon Sep 17 00:00:00 2001 From: WuXianChaoPin <1029559041@qq.com> Date: Sat, 28 Apr 2018 18:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=9B=B8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PixivSearch/{tsdm.py => pixiv/__init__.py} | 0 PixivSearch/{ => pixiv}/pixiv.py | 0 PixivSearch/thrift/TestQry/Client.py | 25 +++ PixivSearch/thrift/TestQry/Server.py | 29 +++ PixivSearch/thrift/TestQry/TestQry-remote | 116 ++++++++++ PixivSearch/thrift/TestQry/TestQry.py | 247 +++++++++++++++++++++ PixivSearch/thrift/TestQry/__init__.py | 1 + PixivSearch/thrift/TestQry/constants.py | 14 ++ PixivSearch/thrift/TestQry/ttypes.py | 91 ++++++++ PixivSearch/thrift/__init__.py | 0 10 files changed, 523 insertions(+) rename PixivSearch/{tsdm.py => pixiv/__init__.py} (100%) rename PixivSearch/{ => pixiv}/pixiv.py (100%) create mode 100644 PixivSearch/thrift/TestQry/Client.py create mode 100644 PixivSearch/thrift/TestQry/Server.py create mode 100755 PixivSearch/thrift/TestQry/TestQry-remote create mode 100644 PixivSearch/thrift/TestQry/TestQry.py create mode 100644 PixivSearch/thrift/TestQry/__init__.py create mode 100644 PixivSearch/thrift/TestQry/constants.py create mode 100644 PixivSearch/thrift/TestQry/ttypes.py create mode 100644 PixivSearch/thrift/__init__.py diff --git a/PixivSearch/tsdm.py b/PixivSearch/pixiv/__init__.py similarity index 100% rename from PixivSearch/tsdm.py rename to PixivSearch/pixiv/__init__.py diff --git a/PixivSearch/pixiv.py b/PixivSearch/pixiv/pixiv.py similarity index 100% rename from PixivSearch/pixiv.py rename to PixivSearch/pixiv/pixiv.py diff --git a/PixivSearch/thrift/TestQry/Client.py b/PixivSearch/thrift/TestQry/Client.py new file mode 100644 index 0000000..2839dc7 --- /dev/null +++ b/PixivSearch/thrift/TestQry/Client.py @@ -0,0 +1,25 @@ +import sys + +from thrift.protocol import TBinaryProtocol +from thrift.transport import TSocket, TTransport + +sys.path.append("/home/hua/PycharmProjects/PixivSearch") +from PixivSearch.thrift.TestQry.TestQry import Client + +if __name__ == '__main__': + # Make socket + socket = TSocket.TSocket('127.0.0.1', 2233) + + # Buffering is critical. Raw sockets are very slow + transport = TTransport.TFramedTransport(socket) + + if not transport.isOpen(): + transport.open() + + # Wrap in a protocol + protocol = TBinaryProtocol.TBinaryProtocol(transport) + + # Create a client to use the protocol encoder + client = Client(protocol) + + print(client.qryTest(0)) diff --git a/PixivSearch/thrift/TestQry/Server.py b/PixivSearch/thrift/TestQry/Server.py new file mode 100644 index 0000000..de53097 --- /dev/null +++ b/PixivSearch/thrift/TestQry/Server.py @@ -0,0 +1,29 @@ +from thrift.protocol import TBinaryProtocol +from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory +from thrift.server.TNonblockingServer import TNonblockingServer +from thrift.transport import TSocket +from thrift.transport.TTransport import TFramedTransport + +from PixivSearch.thrift.TestQry import TestQry +from PixivSearch.thrift.TestQry.ttypes import QryResult + + +class QueryImpl(TestQry.Iface): + + def qryTest(self, qryCode): + result = QryResult() + if qryCode == 1: + result.code = 1 + result.msg = 'success' + else: + result.code = 0 + result.msg = 'fail' + return result + + +if __name__ == '__main__': + socket = TSocket.TSocket(port=2233) + processor = TestQry.Processor(QueryImpl()) + server = TNonblockingServer(processor, socket, TBinaryProtocolFactory(), TFramedTransport()) + + server.serve() diff --git a/PixivSearch/thrift/TestQry/TestQry-remote b/PixivSearch/thrift/TestQry/TestQry-remote new file mode 100755 index 0000000..3a5edd1 --- /dev/null +++ b/PixivSearch/thrift/TestQry/TestQry-remote @@ -0,0 +1,116 @@ +#!/usr/bin/env python +# +# Autogenerated by Thrift Compiler (0.11.0) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +import sys +import pprint +from urllib.parse import urlparse +sys.path.append("/home/hua/PycharmProjects/PixivSearch") +from PixivSearch.thrift import TestQry + +from thrift.transport import TTransport, TSocket, TSSLSocket, THttpClient +from thrift.protocol.TBinaryProtocol import TBinaryProtocol + + + +if len(sys.argv) <= 1 or sys.argv[1] == '--help': + print('') + print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] [-s[sl]] [-novalidate] [-ca_certs certs] [-keyfile keyfile] [-certfile certfile] function [arg1 [arg2...]]') + print('') + print('Functions:') + print(' QryResult qryTest(i32 qryCode)') + print('') + sys.exit(0) + +pp = pprint.PrettyPrinter(indent=2) +host = 'localhost' +port = 9090 +uri = '' +framed = False +ssl = False +validate = True +ca_certs = None +keyfile = None +certfile = None +http = False +argi = 1 + +if sys.argv[argi] == '-h': + parts = sys.argv[argi + 1].split(':') + host = parts[0] + if len(parts) > 1: + port = int(parts[1]) + argi += 2 + +if sys.argv[argi] == '-u': + url = urlparse(sys.argv[argi + 1]) + parts = url[1].split(':') + host = parts[0] + if len(parts) > 1: + port = int(parts[1]) + else: + port = 80 + uri = url[2] + if url[4]: + uri += '?%s' % url[4] + http = True + argi += 2 + +if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed': + framed = True + argi += 1 + +if sys.argv[argi] == '-s' or sys.argv[argi] == '-ssl': + ssl = True + argi += 1 + +if sys.argv[argi] == '-novalidate': + validate = False + argi += 1 + +if sys.argv[argi] == '-ca_certs': + ca_certs = sys.argv[argi+1] + argi += 2 + +if sys.argv[argi] == '-keyfile': + keyfile = sys.argv[argi+1] + argi += 2 + +if sys.argv[argi] == '-certfile': + certfile = sys.argv[argi+1] + argi += 2 + +cmd = sys.argv[argi] +args = sys.argv[argi + 1:] + +if http: + transport = THttpClient.THttpClient(host, port, uri) +else: + if ssl: + socket = TSSLSocket.TSSLSocket(host, port, validate=validate, ca_certs=ca_certs, keyfile=keyfile, certfile=certfile) + else: + socket = TSocket.TSocket(host, port) + if framed: + transport = TTransport.TFramedTransport(socket) + else: + transport = TTransport.TBufferedTransport(socket) +protocol = TBinaryProtocol(transport) +client = TestQry.Client(protocol) +transport.open() + +if cmd == 'qryTest': + if len(args) != 1: + print('qryTest requires 1 args') + sys.exit(1) + pp.pprint(client.qryTest(eval(args[0]),)) + +else: + print('Unrecognized method %s' % cmd) + sys.exit(1) + +transport.close() diff --git a/PixivSearch/thrift/TestQry/TestQry.py b/PixivSearch/thrift/TestQry/TestQry.py new file mode 100644 index 0000000..4114cd0 --- /dev/null +++ b/PixivSearch/thrift/TestQry/TestQry.py @@ -0,0 +1,247 @@ +# +# Autogenerated by Thrift Compiler (0.11.0) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# +import logging + +from thrift.TRecursive import fix_spec +from thrift.Thrift import TProcessor +from thrift.Thrift import TType, TMessageType, TApplicationException +from thrift.transport import TTransport + +from PixivSearch.thrift.TestQry.ttypes import QryResult + +all_structs = [] + +port=2233 + +class Iface(object): + def qryTest(self, qryCode): + """ + 测试查询接口,当qryCode值为1时返回"成功"的响应信息,qryCode值为其他值时返回"失败"的响应信息 + @param qryCode测试参数 + + Parameters: + - qryCode + """ + pass + + +class Client(Iface): + def __init__(self, iprot, oprot=None): + self._iprot = self._oprot = iprot + if oprot is not None: + self._oprot = oprot + self._seqid = 0 + + def qryTest(self, qryCode): + """ + 测试查询接口,当qryCode值为1时返回"成功"的响应信息,qryCode值为其他值时返回"失败"的响应信息 + @param qryCode测试参数 + + Parameters: + - qryCode + """ + self.send_qryTest(qryCode) + return self.recv_qryTest() + + def send_qryTest(self, qryCode): + self._oprot.writeMessageBegin('qryTest', TMessageType.CALL, self._seqid) + args = qryTest_args() + args.qryCode = qryCode + args.write(self._oprot) + self._oprot.writeMessageEnd() + self._oprot.trans.flush() + + def recv_qryTest(self): + iprot = self._iprot + (fname, mtype, rseqid) = iprot.readMessageBegin() + if mtype == TMessageType.EXCEPTION: + x = TApplicationException() + x.read(iprot) + iprot.readMessageEnd() + raise x + result = qryTest_result() + result.read(iprot) + iprot.readMessageEnd() + if result.success is not None: + return result.success + raise TApplicationException(TApplicationException.MISSING_RESULT, "qryTest failed: unknown result") + + +class Processor(Iface, TProcessor): + def __init__(self, handler): + self._handler = handler + self._processMap = {} + self._processMap["qryTest"] = Processor.process_qryTest + + def process(self, iprot, oprot): + (name, type, seqid) = iprot.readMessageBegin() + if name not in self._processMap: + iprot.skip(TType.STRUCT) + iprot.readMessageEnd() + x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name)) + oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid) + x.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + return + else: + self._processMap[name](self, seqid, iprot, oprot) + return True + + def process_qryTest(self, seqid, iprot, oprot): + args = qryTest_args() + args.read(iprot) + iprot.readMessageEnd() + result = qryTest_result() + try: + result.success = self._handler.qryTest(args.qryCode) + msg_type = TMessageType.REPLY + except TTransport.TTransportException: + raise + except TApplicationException as ex: + logging.exception('TApplication exception in handler') + msg_type = TMessageType.EXCEPTION + result = ex + except Exception: + logging.exception('Unexpected exception in handler') + msg_type = TMessageType.EXCEPTION + result = TApplicationException(TApplicationException.INTERNAL_ERROR, 'Internal error') + oprot.writeMessageBegin("qryTest", msg_type, seqid) + result.write(oprot) + oprot.writeMessageEnd() + oprot.trans.flush() + +# HELPER FUNCTIONS AND STRUCTURES + + +class qryTest_args(object): + """ + Attributes: + - qryCode + """ + + + def __init__(self, qryCode=None,): + self.qryCode = qryCode + + def read(self, iprot): + if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None: + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.I32: + self.qryCode = iprot.readI32() + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec])) + return + oprot.writeStructBegin('qryTest_args') + if self.qryCode is not None: + oprot.writeFieldBegin('qryCode', TType.I32, 1) + oprot.writeI32(self.qryCode) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.items()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) +all_structs.append(qryTest_args) +qryTest_args.thrift_spec = ( + None, # 0 + (1, TType.I32, 'qryCode', None, None, ), # 1 +) + + +class qryTest_result(object): + """ + Attributes: + - success + """ + + + def __init__(self, success=None,): + self.success = success + + def read(self, iprot): + if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None: + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 0: + if ftype == TType.STRUCT: + self.success = QryResult() + self.success.read(iprot) + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec])) + return + oprot.writeStructBegin('qryTest_result') + if self.success is not None: + oprot.writeFieldBegin('success', TType.STRUCT, 0) + self.success.write(oprot) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.items()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) +all_structs.append(qryTest_result) +qryTest_result.thrift_spec = ( + (0, TType.STRUCT, 'success', [QryResult, None], None, ), # 0 +) +fix_spec(all_structs) +del all_structs + + + + diff --git a/PixivSearch/thrift/TestQry/__init__.py b/PixivSearch/thrift/TestQry/__init__.py new file mode 100644 index 0000000..22182f6 --- /dev/null +++ b/PixivSearch/thrift/TestQry/__init__.py @@ -0,0 +1 @@ +__all__ = ['ttypes', 'constants', 'TestQry'] diff --git a/PixivSearch/thrift/TestQry/constants.py b/PixivSearch/thrift/TestQry/constants.py new file mode 100644 index 0000000..0c217ce --- /dev/null +++ b/PixivSearch/thrift/TestQry/constants.py @@ -0,0 +1,14 @@ +# +# Autogenerated by Thrift Compiler (0.11.0) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException +from thrift.protocol.TProtocol import TProtocolException +from thrift.TRecursive import fix_spec + +import sys +from .ttypes import * diff --git a/PixivSearch/thrift/TestQry/ttypes.py b/PixivSearch/thrift/TestQry/ttypes.py new file mode 100644 index 0000000..523a81b --- /dev/null +++ b/PixivSearch/thrift/TestQry/ttypes.py @@ -0,0 +1,91 @@ +# +# Autogenerated by Thrift Compiler (0.11.0) +# +# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING +# +# options string: py +# + +from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException +from thrift.protocol.TProtocol import TProtocolException +from thrift.TRecursive import fix_spec + +import sys + +from thrift.transport import TTransport +all_structs = [] + + +class QryResult(object): + """ + Attributes: + - code: 返回码, 1成功,0失败 + - msg: 响应信息 + """ + + + def __init__(self, code=None, msg=None,): + self.code = code + self.msg = msg + + def read(self, iprot): + if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None: + iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec]) + return + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.I32: + self.code = iprot.readI32() + else: + iprot.skip(ftype) + elif fid == 2: + if ftype == TType.STRING: + self.msg = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString() + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + if oprot._fast_encode is not None and self.thrift_spec is not None: + oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec])) + return + oprot.writeStructBegin('QryResult') + if self.code is not None: + oprot.writeFieldBegin('code', TType.I32, 1) + oprot.writeI32(self.code) + oprot.writeFieldEnd() + if self.msg is not None: + oprot.writeFieldBegin('msg', TType.STRING, 2) + oprot.writeString(self.msg.encode('utf-8') if sys.version_info[0] == 2 else self.msg) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() + + def validate(self): + return + + def __repr__(self): + L = ['%s=%r' % (key, value) + for key, value in self.__dict__.items()] + return '%s(%s)' % (self.__class__.__name__, ', '.join(L)) + + def __eq__(self, other): + return isinstance(other, self.__class__) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not (self == other) +all_structs.append(QryResult) +QryResult.thrift_spec = ( + None, # 0 + (1, TType.I32, 'code', None, None, ), # 1 + (2, TType.STRING, 'msg', 'UTF8', None, ), # 2 +) +fix_spec(all_structs) +del all_structs diff --git a/PixivSearch/thrift/__init__.py b/PixivSearch/thrift/__init__.py new file mode 100644 index 0000000..e69de29