Python socket

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

Socket

Socket接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,用以开发TCP/IP网络上的应用程序.

socket server

import socket

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

    def start_server(self):
        add = (self.host, self.port)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(add)
        sock.listen(5)


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

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

socket Client

import socket

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

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

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

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

RPC

Thrift

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 
    __init__.py	
    time_service
        __init__.py		
        constants.py		
        timeServe-remote	
        timeServe.py		
        ttypes.py

server.py

import sys
import time

from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TTransport
sys.path.append('gen-py')
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='127.0.0.1', port=9090)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

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

    print('Starting the server...')
    server.serve()
    print('done.')

python server.py

Starting the server…

client.py

import sys

from thrift import Thrift
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
sys.path.append('gen-py')
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!
    transport.open()

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

    # Close!
    transport.close()


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

python client.py

Client Received Sat Aug 8 00:05:56 2018

refs

python thrift: https://www.hardikp.com/2018/07/28/services/




本博客所有文章采用的授权方式为 自由转载-非商用-非衍生-保持署名 ,转载请务必注明出处,谢谢。
声明:
本博客欢迎转发,但请保留原作者信息!
博客地址:邱文奇(qiuwenqi)的博客;
内容系本人学习、研究和总结,如有雷同,实属荣幸!
阅读次数:

文章评论

comments powered by Disqus


章节列表