Python socket

发布于 2018-01-08 · 本文总共 6015 字 · 阅读大约需要 18 分钟



socket server

import socket

class Server():
    def __init__(self, host, port): = host
        self.port = port

    def start_server(self):
        add = (, self.port)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        while True:
            con, addr = sock.accept()

            while True:
                data = con.recv(2048)
                if len(data) > 0:
                    msg = "server recv data: {}.".format(data)
                    con.sendall("i have received: {}".format(data).encode("utf-8"))
                    print("received None")

socket Client

import socket

class Client():
    def __init__(self, host, port): = host
        self.port = port

    def send(self):
        add = (, self.port)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        msg = "hello world"
        data = sock.recv(2048)
        res = "client receive msg: {}".format(data)

if __name__ == '__main__':
    client = Client("", 8100)



Thrift is an interface definition language and binary communication protocol used for defining and creating services for numerous languages. It forms a remote procedure call (RPC) framework and was developed at Facebook for “scalable cross-language services development”. It combines a software stack with a code generation engine to build cross-platform services which can connect applications written in a variety of languages and frameworks, including ActionScript, C, C++, C#, Cappuccino, Cocoa, Delphi, Erlang, Go, Haskell, Java, JavaScript, Objective-C, OCaml, Perl, PHP, Python, Ruby, Elixir, Rust, Smalltalk and Swift. Although developed at Facebook, it is now an open source project in the Apache Software Foundation. The implementation was described in an April 2007 technical paper released by Facebook, now hosted on Apache.

  • Architecture

The Apache Thrift API client/server architecture Thrift includes a complete stack for creating clients and servers. The top part is generated code from the Thrift definition. From this file, the services generate client and processor code. In contrast to built-in types, created data structures are sent as result in generated code. The protocol and transport layer are part of the runtime library. With Thrift, it is possible to define a service and change the protocol and transport without recompiling the code. Besides the client part, Thrift includes server infrastructure to tie protocols and transports together, like blocking, non-blocking, and multi-threaded servers. The underlying I/O part of the stack is implemented differently for different languages.

Thrift supports a number of protocols:

TBinaryProtocol – A straightforward binary format, simple, but not optimized for space efficiency. Faster to process than the text protocol but more difficult to debug.

TCompactProtocol – More compact binary format; typically more efficient to process as well

TJSONProtocol – Uses JSON for encoding of data.

TSimpleJSONProtocol – A write-only protocol that cannot be parsed by Thrift because it drops metadata using JSON. Suitable for parsing by scripting languages.

The supported transports are:

TSimpleFileTransport – This transport writes to a file.

TFramedTransport – This transport is required when using a non-blocking server. It sends data in frames, where each frame is preceded by length information.

TMemoryTransport – Uses memory for I/O. The Java implementation uses a simple ByteArrayOutputStream internally.

TSocket – Uses blocking socket I/O for transport.

TZlibTransport – Performs compression using zlib. Used in conjunction with another transport.

Thrift also provides a number of servers, which are

TNonblockingServer – A multi-threaded server using non-blocking I/O (Java implementation uses NIO channels). TFramedTransport must be used with this server.

TSimpleServer – A single-threaded server using standard blocking I/O. Useful for testing.

TThreadedServer – A multi-threaded server using a thread per connection model and standard blocking I/O.

TThreadPoolServer – A multi-threaded server using a thread pool and standard blocking I/O.

  • Benefits

Some stated benefits of Thrift include:[citation needed]

Cross-language serialization with lower overhead than alternatives such as SOAP due to use of binary format.

A lean and clean library. No framework to code. No XML configuration files.

The language bindings feel natural. For example, Java uses ArrayList. C++ uses std::vector<std::string>.

The application-level wire format and the serialization-level wire format are cleanly separated. They can be modified independently.

The predefined serialization styles include: binary, HTTP-friendly and compact binary.

Doubles as cross-language file serialization.

Soft versioning[clarify] of the protocol. Thrift does not require a centralized and explicit mechanism like major-version/minor-version. Loosely coupled teams can freely evolve RPC calls.

No build dependencies or non-standard software. No mix of incompatible software licenses.

simple example

vi time_service.thrift

service timeServe {
    string getCurrtentTime()

thrift -r –gen py time_service.thrift

ls gen-py	

import sys
import time

from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TTransport
from time_service import timeServe

class TimeHandler:
    def __init__(self):
        self.log = {}

    def getCurrtentTime(self):
        return time.ctime()

if __name__ == '__main__':
    handler = TimeHandler()
    processor = timeServe.Processor(handler)
    transport = TSocket.TServerSocket(host='', port=9090)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    print('Starting the server...')


Starting the server…

import sys

from thrift import Thrift
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
from time_service import timeServe

def main():
    # Make socket
    transport = TSocket.TSocket('localhost', 9090)

    # Buffering is critical. Raw sockets are very slow
    transport = TTransport.TBufferedTransport(transport)

    # Wrap in a protocol
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    # Create a client to use the protocol encoder
    client = timeServe.Client(protocol)

    # Connect!

    ts = client.getCurrtentTime()
    print('Client Received {}'.format(ts))

    # Close!

if __name__ == '__main__':
    except Thrift.TException as tx:
        print('%s' % tx.message)


Client Received Sat Aug 8 00:05:56 2018


python thrift:

本博客所有文章采用的授权方式为 自由转载-非商用-非衍生-保持署名 ,转载请务必注明出处,谢谢。


comments powered by Disqus