2014-06-09 1 views
1

여기 sendQuote 메서드에서 바이트 배열을 보내고 싶지만 twisted는 문자열을 취하는 전송 쓰기 메서드를 제공합니다. 어떻게하면 Twisted를 사용하여 응답으로 바이트 배열을 보낼 수 있습니다. 감사.twisted python을 사용하여 바이트 배열을 보내는 방법

class QuoteProtocol(protocol.Protocol): 
    def __init__(self, factory): 
    self.factory = factory 

    def connectionMade(self): 
    self.sendQuote() 

    def sendQuote(self): 
    #self.file.write(bytearray([0x00, 0x31, 0x34, 0x32, 0x30, 0x30, 0x30, 0x30, 0x31, 0x11, 0x0c, 0x00, 0xfd, 0x09, 0x00, 0x2f, 0xe7, 0x5e, 0x3a, 0x08, 0x3c, 0x00, 0x00, 0x00, 0x49, 0x95])) 
    self.transport.write("Quote reecevied") 

    def dataReceived(self, data): 
    print "Received quote:", data 
    self.transport.loseConnection() 

답변

2

bytearray을 보내시겠습니까? 파이썬의 네이티브 문자열 타입은 사실상 바이트 배열입니다. 이미 살펴본 것처럼 transport.write은 문자열을 허용합니다. 따라서 전송해야하는 모든 바이트 배열을 보낼 수 있습니다.

bytearray 인스턴스가 이미있는 경우 몇 가지 이유가 있습니다. bytes(your_bytearray)을 사용하면 bytes 인스턴스를 쉽게 생성 할 수 있습니다.

관련 문제