当前位置: 首页>>代码示例>>Python>>正文


Python Connection.write方法代码示例

本文整理汇总了Python中OpenSSL.SSL.Connection.write方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.write方法的具体用法?Python Connection.write怎么用?Python Connection.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在OpenSSL.SSL.Connection的用法示例。


在下文中一共展示了Connection.write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SocketClient

# 需要导入模块: from OpenSSL.SSL import Connection [as 别名]
# 或者: from OpenSSL.SSL.Connection import write [as 别名]
class SocketClient(object):
    """This class sends all info to the server
    """

    cacertpath = "ca/cacert.pem"
    BUFF = 8192

    def __init__(self,HOST='130.236.219.232', PORT = 443):
        self.mutex = threading.Semaphore(1)
        self.connected = False
        self.connect()
        self.host_addr = HOST
        self.host_port = PORT

    def connect(self):
        print "You are trying to connect..."
        for x in range(7):
            if not self.connected:
                try:
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    context = Context(TLSv1_METHOD)
                    context.use_certificate_file(self.cacertpath)
                    context.set_timeout(2)
                    self.sslsocket = Connection(context,s)
                    self.sslsocket.connect((self.host_addr,self.host_port))
                    #starting a thread that listen to what server sends which the clients need to be able to send and recive data at the same time
                    t = threading.Thread(target=self.receive)
                    t.daemon = True
                    t.start()
                    if self.sslsocket:
                        self.connected = True
                    print "connection established"
                    #self.authentication("Kalle", "te")
                    t = threading.Thread(target=self.sendinput)
                    t.start()
                except socket.error:
                    print "You failed to connect, retrying......."
                    time.sleep(5)

    def authentication(self, username, password):
        self.sslsocket.send(username)
        self.sslsocket.send(password)

    #sending string to server
    def send(self,str):
        try:
            self.sslsocket.write("start")
            totalsent =  0
            while totalsent < str.__len__():
                sent = self.sslsocket.write(str[totalsent:])
                if sent == 0:
                    raise RuntimeError, "socket connection broken"
                totalsent = totalsent + sent
            self.sslsocket.write("end")
        except SSL.SysCallError:
            print "your server is dead, you have to resend data"
            self.connected = False
            self.sslsocket.shutdown()
            self.sslsocket.close()
            self.mutex.acquire()
            print "Du är inne i connect via send SysCallError"
            self.connect()
            self.mutex.release()
        except SSL.Error:
            self.connected = False
            self.mutex.acquire()
            print "Du är inne i connect via send ssl error"
            self.connect()
            self.mutex.release()

    #Sending input to server
    def sendinput(self):
        try:
            while True:
                input = raw_input()
                self.send(input)
        except KeyboardInterrupt:
            print "du är inne i sendinput"
            self.sslsocket.shutdown()
            self.sslsocket.close()
            exit(0)

    #getting data from server
    def receive(self):
        output = ""
        try:
            while True:
                data = self.sslsocket.recv(self.BUFF)
                if data == "start":
                    while True: 
                        data = self.sslsocket.recv(self.BUFF)
                        if data == "end":
                            print output
                            output = ""
                            break
                        output = output + data
        except SSL.SysCallError:
            print "OMG Server is down"
            self.connected = False
            print self.connected
#.........这里部分代码省略.........
开发者ID:dreamwave,项目名称:rad,代码行数:103,代码来源:socketclient.py


注:本文中的OpenSSL.SSL.Connection.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。