Exemple #1
0
    def serve(self, socket):
        with self.lock:
            self.socket = socket
            sendall = self.socket.sendall
            readline = self.socket.makefile().readline

        try:
            while self.socket:
                self.limit.acquire()
                # Idea: Send first, receive later. If the client is to
                # slow to get the send-buffer empty, he cannot send.
                while self.sendbuffer:
                    sendall(self.sendbuffer.popleft())
                line = readline().strip()
                if not line:
                    break
                arguments = line.split()
                command = arguments.pop(0)
                try:
                    self.canvas.fire('COMMAND-%s' % command.upper(), self, *arguments)
                except Exception, e:
                    socket.send('ERROR %r :(' % e)
                    break
        finally:
            self.disconnect()
Exemple #2
0
def odbierz_dane(socket):
    rozmiar = int(socket.recv(1024))
    if rozmiar != 0:
        socket.send("1")
        dane = socket.recv(rozmiar)
        return dane
    else:
        socket.send("0")
        print "Problem z odbiorem danych!"
Exemple #3
0
def wyslij_dane(socket, dane):
    dlugosc = str(len(dane))
    socket.send(dlugosc)
    ack = socket.recv(3)
    ack = int(ack)
    if ack == 1:
        socket.send(dane)
    else:
        print "Mamy problem - serwer nie odpowiada na wiadomosc o dlugosci danych!"
Exemple #4
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError as x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._read_event)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._write_event)
                 else:
                     raise
             else:
                 return v
     else:
         return socket.send(self, data, flags, timeout)
Exemple #5
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError:
                 x = sys.exc_info()[1]
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._read_event)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     self._wait(self._write_event)
                 else:
                     raise
             else:
                 return v
     else:
         return socket.send(self, data, flags, timeout)
Exemple #6
0
 def send(self, data, flags=0, timeout=timeout_default):
     self._checkClosed()
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError("non-zero flags not allowed in calls to send() on %s" % self.__class__)
         while True:
             try:
                 return self._sslobj.write(data)
             except SSLWantReadError:
                 if self.timeout == 0.0:
                     return 0
                 self._wait(self._read_event)
             except SSLWantWriteError:
                 if self.timeout == 0.0:
                     return 0
                 self._wait(self._write_event)
     else:
         return socket.send(self, data, flags, timeout)
Exemple #7
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError as e:
                 errno = e.args[0]
                 if errno in [SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE] and timeout == 0.0:
                     return 0
                 self._handle_wait_exc(e, timeout)
             else:
                 return v
     else:
         return socket.send(self, data, flags, timeout)
Exemple #8
0
    def send(self, data, flags=0, timeout=timeout_default):
        self._checkClosed()
        self.__check_flags('send', flags)

        if timeout is timeout_default:
            timeout = self.timeout

        if not self._sslobj:
            return socket.send(self, data, flags, timeout)

        while True:
            try:
                return self._sslobj.write(data)
            except SSLWantReadError:
                if self.timeout == 0.0:
                    return 0
                self._wait(self._read_event)
            except SSLWantWriteError:
                if self.timeout == 0.0:
                    return 0
                self._wait(self._write_event)
Exemple #9
0
    def send(self, data, flags=0, timeout=timeout_default):
        self._checkClosed()
        self.__check_flags('send', flags)

        if timeout is timeout_default:
            timeout = self.timeout

        if not self._sslobj:
            return socket.send(self, data, flags, timeout)

        while True:
            try:
                return self._sslobj.write(data)
            except SSLWantReadError:
                if self.timeout == 0.0:
                    return 0
                self._wait(self._read_event)
            except SSLWantWriteError:
                if self.timeout == 0.0:
                    return 0
                self._wait(self._write_event)
 def send(self, data, flags=0, timeout=timeout_default):
     self._checkClosed()
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 return self._sslobj.write(data)
             except SSLWantReadError:
                 if self.timeout == 0.0:
                     return 0
                 self._wait(self._read_event)
             except SSLWantWriteError:
                 if self.timeout == 0.0:
                     return 0
                 self._wait(self._write_event)
     else:
         return socket.send(self, data, flags, timeout)
Exemple #11
0
                    if x.args[0] == SSL_ERROR_WANT_READ:
                        if self.timeout == 0.0:
                            return 0
                        sys.exc_clear()
                        self._wait(self._read_event)
                    elif x.args[0] == SSL_ERROR_WANT_WRITE:
                        if self.timeout == 0.0:
                            return 0
                        sys.exc_clear()
                        self._wait(self._write_event)
                    else:
                        raise
                else:
                    return v
        else:
            return socket.send(self, data, flags, timeout)
    # is it possible for sendall() to send some data without encryption if another end shut down SSL?

    def sendto(self, *args):
        if self._sslobj:
            raise ValueError("sendto not allowed on instances of %s" %
                             self.__class__)
        else:
            return socket.sendto(self, *args)

    def recv(self, buflen=1024, flags=0):
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to recv() on %s" %
                    self.__class__)
Exemple #12
0
                    if x.args[0] == SSL_ERROR_WANT_READ:
                        if self.timeout == 0.0:
                            raise timeout(str(x))
                        sys.exc_clear()
                        wait_read(self.fileno(), timeout=timeout)
                    elif x.args[0] == SSL_ERROR_WANT_WRITE:
                        if self.timeout == 0.0:
                            raise timeout(str(x))
                        sys.exc_clear()
                        wait_write(self.fileno(), timeout=timeout)
                    else:
                        raise
                else:
                    return v
        else:
            return socket.send(self, data, flags, timeout)
    # is it possible for sendall() to send some data without encryption if another end shut down SSL?

    def sendto(self, data, addr, flags=0):
        if self._sslobj:
            raise ValueError("sendto not allowed on instances of %s" %
                             self.__class__)
        else:
            return socket.sendto(self, data, addr, flags)

    def recv(self, buflen=1024, flags=0):
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to recv() on %s" %
                    self.__class__)