コード例 #1
0
    def makefile(self, mode='r', bufsize=-1):
        """Create a file object for the TLS connection (socket emulation).

        :rtype: socket._fileobject
        """
        self._refCount += 1
        # So, it is pretty fragile to be using Python internal objects
        # like this, but it is probably the best/easiest way to provide
        # matching behavior for socket emulation purposes.  The 'close'
        # argument is nice, its apparently a recent addition to this
        # class, so that when fileobject.close() gets called, it will
        # close() us, causing the refcount to be decremented (decrefAsync).
        #
        # If this is the last close() on the outstanding fileobjects / 
        # TLSConnection, then the "actual" close alerts will be sent,
        # socket closed, etc.

        # for writes, we MUST buffer otherwise the lengths of headers leak
        # through record layer boundaries
        if 'w' in mode and bufsize <= 0:
            bufsize = 2**14

        if sys.version_info < (3,):
            return socket._fileobject(self, mode, bufsize, close=True)
        else:
            if 'w' in mode:
                return io.BufferedWriter(socket.SocketIO(self, mode), bufsize)
            else:
                return socket.SocketIO(self, mode)
コード例 #2
0
	def setup(self):
		if self.server.use_ssl:
			self.connection = self.request
			self.rfile = socket.SocketIO(self.request, "rb")
			self.wfile = socket.SocketIO(self.request, "wb")
		else:
			StreamRequestHandler.setup(self)
		self.keep_alive = True
		self.handshake_done = False
		self.valid_client = False
コード例 #3
0
    def setup(self):
        # added a try except block in case of transmission errors
        try:

            self.connection = self.request
            self.rfile = socket.SocketIO(self.request, "rb")
            self.wfile = socket.SocketIO(self.request, "wb")

        # except errors and pass them
        except:
            pass
コード例 #4
0
 def _makefile(self):
     """
     Set up .rfile and .wfile attributes from .connection
     """
     # Ideally, we would use the Buffered IO in Python 3 by default.
     # Unfortunately, the implementation of .peek() is broken for n>1 bytes,
     # as it may just return what's left in the buffer and not all the bytes we want.
     # As a workaround, we just use unbuffered sockets directly.
     # https://mail.python.org/pipermail/python-dev/2009-June/089986.html
     self.rfile = Reader(socket.SocketIO(self.connection, "rb"))
     self.wfile = Writer(socket.SocketIO(self.connection, "wb"))
コード例 #5
0
 def listen(self):
     logger.debug('listening')
     with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
         while self.ready:
             conn, addr = self.listenSoc.accept()
             logger.debug('received connection')
             sw = socket.SocketIO(conn, 'w')
             self.out = io.BufferedWriter(sw)
             sr = socket.SocketIO(conn, 'r')
             inp = KQMLReader(io.BufferedReader(sr))
             self.dispatcher = KQMLDispatcher(self, inp, self.name)
             executor.submit(self.dispatcher.start)
コード例 #6
0
ファイル: kqml_module.py プロジェクト: metincansiper/pykqml
 def connect1(self, host, port, verbose=True):
     try:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.socket.connect((host, port))
         sw = socket.SocketIO(self.socket, 'w')
         self.out = io.BufferedWriter(sw)
         sr = socket.SocketIO(self.socket, 'r')
         self.inp = KQMLReader(io.BufferedReader(sr))
         return True
     except socket.error as e:
         if verbose:
             logger.error(e)
         return False
コード例 #7
0
ファイル: servefile.py プロジェクト: fjcloud/servefile
	def setup(self):
		self.connection = self.request

		if sys.version_info[0] > 2:
			# python3 SocketIO (replacement for socket._fileobject)
			raw_read_sock = socket.SocketIO(self.request, 'rb')
			raw_write_sock = socket.SocketIO(self.request, 'wb')
			rbufsize = self.rbufsize > 0 and self.rbufsize or io.DEFAULT_BUFFER_SIZE
			wbufsize = self.wbufsize > 0 and self.wbufsize or io.DEFAULT_BUFFER_SIZE
			self.rfile = io.BufferedReader(raw_read_sock, rbufsize)
			self.wfile = io.BufferedWriter(raw_write_sock, wbufsize)
		else:
			# python2 does not have SocketIO
			self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
			self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
コード例 #8
0
 def connect_tcp_client(self,
                        host,
                        port=16107,
                        ssl_protocol=None,
                        timeout=None,
                        verify_mode=ssl.CERT_OPTIONAL,
                        ca_cert=None,
                        certfile=None,
                        keyfile=None,
                        cert_pass=None):
     slog = getSyslog()
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     if ssl_protocol == True:
         raw_sock = ssl.wrap_socket(s,
                                    keyfile=keyfile,
                                    certfile=certfile,
                                    cert_reqs=verify_mode,
                                    ca_certs=ca_cert)
         raw_sock.settimeout(10)
         self.ssl = True
     else:
         raw_sock = s
     raw_sock.connect((host, port))
     if ssl_protocol != None:
         slog.logsys('Server cert is', raw_sock.getpeercert())
     slog.logsys('Connected to', host, port)
     if timeout != None: raw_sock.settimeout(timeout)
     else: raw_sock.settimeout(None)
     self.__connection = socket.SocketIO(raw_sock, "rwb")
     self.__raw_socket = raw_sock
     self.__timeout = timeout
コード例 #9
0
def test_read_only_stream_is_listened_as_writable(loop, mock, socket_pair):
    read_only = socket.SocketIO(socket_pair[0], 'rb')
    assert read_only.readable()
    assert not read_only.writable()
    loop.add_write_stream(read_only, mock)
    loop.next_tick()
    mock.assert_called_once()
コード例 #10
0
ファイル: pima.py プロジェクト: adi-miller/Alarm
 def __init__(self,
              zones: int,
              serialport: str = None,
              ipaddr: str = None,
              ipport: int = None) -> None:
     if serialport is not None:
         try:
             self._channel = serial.Serial(port=serialport,
                                           baudrate=2400,
                                           bytesize=serial.EIGHTBITS,
                                           parity=serial.PARITY_NONE,
                                           timeout=1)
         except (termios.error, serial.serialutil.SerialException) as e:
             self._channel = None
             raise Error('Failed to connect to serial port.') from e
     else:
         try:
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             sock.connect((ipaddr, ipport))
             self._channel = socket.SocketIO(sock, 'rwb')
         except (socket.error, socket.gaierror) as e:
             self._channel = None
             raise Error('Error creating socket.') from e
     self._crc = crcmod.mkCrcFun(0x18005,
                                 rev=True,
                                 initCrc=0x0000,
                                 xorOut=0x0000)
     self._zones = zones  # type: int
     self._module_id = self._ZONES_TO_MODULE_ID[self._zones]  # type: bytes
コード例 #11
0
def test_write_only_stream_is_listened_as_readable(loop, mock, socket_pair):
    write_only = socket.SocketIO(socket_pair[0], 'wb')
    assert not write_only.readable()
    assert write_only.writable()
    loop.add_read_stream(write_only, mock)
    socket_pair[1].send(b"foo")
    loop.next_tick()
    mock.assert_called_once()
コード例 #12
0
 def __init__(self, host, port):
     self.socket = socket.socket()
     self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
     self.socket.connect((host, port))
     self.reader = io.BufferedReader(socket.SocketIO(self.socket, 'r'))
     self.writer = self.socket.makefile('w')
     self.writer.write("json\n")
     self.writer.flush()
コード例 #13
0
 def connect1(self, host, port):
     try:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.socket.connect((host, port))
         self.out = io.BufferedWriter(socket.SocketIO(self.socket, 'w'))
         return True
     except socket.error as e:
         LOGGER.error('Connection failed: %s', e)
         return False
コード例 #14
0
 def makefile(self, mode='rb', bufsize=-1):
     # type: (AnyStr, int) -> Union[io.BufferedRWPair,io.BufferedReader]
     if six.PY3:
         raw = socket.SocketIO(self, mode)
         if 'rw' in mode:
             return io.BufferedRWPair(raw, raw)
         return io.BufferedReader(raw)
     else:
         return socket._fileobject(self, mode, bufsize)
コード例 #15
0
 def create_readline_wrapper(fh):
     fh.recv = fh.read
     if is_py2:
         ans = socket._fileobject(fh, close=True)
     else:
         fh.recv_into = fh.readinto
         fh._decref_socketios = lambda: None
         ans = BufferedReader(socket.SocketIO(fh, 'r'))
     return ans
コード例 #16
0
    def makefile(self, mode='r', bufsize=-1):
        self._makefile_refs += 1
        if PY3:
            if bufsize == -1:
                bufsize = io.DEFAULT_BUFFER_SIZE
            raw = socket.SocketIO(self, 'rw')
            return BytesIOWrapper(raw, bufsize)

        # pylint: disable=W0212
        return socket._fileobject(self, mode, bufsize, close=True)
コード例 #17
0
 def receive_eof(self):
     connection, _ = self.listen_socket.accept()
     print(connection)
     # socket_write = socket.SocketIO(connection, 'w')
     # self.out = io.BufferedWriter(socket_write)
     socket_read = socket.SocketIO(connection, 'r')
     inp_reader = KQMLReader(io.BufferedReader(socket_read))
     # self.inp = inp_reader
     self.dispatcher = KQMLDispatcher(self, inp_reader, self.name)
     self.dispatcher.start()
コード例 #18
0
ファイル: trips_module.py プロジェクト: wdebeaum/cogent
 def connect1(self, host, port, verbose=True):
     try:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.socket.connect((host, port))
         if hasattr(socket, 'SocketIO'):
             sw = socket.SocketIO(self.socket, 'w')
             self.out = io.BufferedWriter(sw)
             sr = socket.SocketIO(self.socket, 'r')
             self.inp = KQMLReader(io.BufferedReader(sr))
         else:
             sfn = self.socket.makefile().fileno()
             fio = io.FileIO(sfn, mode='w')
             self.out = io.BufferedWriter(fio)
             fio = io.FileIO(sfn, mode='r')
             self.inp = KQMLReader(io.BufferedReader(fio))
         return True
     except socket.error as e:
         if verbose:
             logger.error(e)
         return False
コード例 #19
0
    def _listen(self):
        """Sets up input and output socket connections to our listener.

        Infinite loop while ready to connect to our listener socket. On connect
        we get the write socket as a Buffered Writer and the read socket as a
        KQML Reader (which passes through a Buffered Reader). The reader is
        then attached to a KQML Dispatcher which is subsequently started, and
        passed along to the executor (which is a thread pool manager).
        """
        LOGGER.debug('listening')
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
            while self.ready:
                connection, _ = self.listen_socket.accept()
                LOGGER.debug('received connection')
                socket_write = socket.SocketIO(connection, 'w')
                self.out = io.BufferedWriter(socket_write)
                socket_read = socket.SocketIO(connection, 'r')
                read_input = KQMLReader(io.BufferedReader(socket_read))
                self.dispatcher = KQMLDispatcher(self, read_input, self.name)
                executor.submit(self.dispatcher.start)
コード例 #20
0
def test_write_to_the_read_only_stream(loop, mock, socket_pair):
    read_only = socket.SocketIO(socket_pair[0], 'rb')

    def collapse(stream):
        try:
            stream.write(b"foo")
        except io.UnsupportedOperation:
            mock()

    loop.add_write_stream(read_only, collapse)
    loop.next_tick()
    mock.assert_called_once()
コード例 #21
0
 def _stream_logs(self, tail, kw):
     # Check that we're properly monkeypatched.
     assert self._api._multiplexed_response_stream_helper != 'ORIG_MRSH'
     assert self._feeder is None
     assert kw == self._expected_stream_params
     server, client = socket.socketpair()
     self._client_sockets.add(client)
     self._feeder = LogFeeder(self, server, self._tail_logs(tail))
     # Add a raw attr for the client to read from.
     self._feeder.raw = socket.SocketIO(client, 'rb')
     self._feeder.start()
     return self._feeder, client
コード例 #22
0
    def makefile(
        self,
        mode: str,
        buffering: Optional[int] = None,
        *,
        encoding: Optional[str] = None,
        errors: Optional[str] = None,
        newline: Optional[str] = None,
    ) -> Union[BinaryIO, TextIO, socket.SocketIO]:
        """
        Python's httpclient uses makefile and buffered io when reading HTTP
        messages and we need to support it.

        This is unfortunately a copy and paste of socket.py makefile with small
        changes to point to the socket directly.
        """
        if not set(mode) <= {"r", "w", "b"}:
            raise ValueError(f"invalid mode {mode!r} (only r, w, b allowed)")

        writing = "w" in mode
        reading = "r" in mode or not writing
        assert reading or writing
        binary = "b" in mode
        rawmode = ""
        if reading:
            rawmode += "r"
        if writing:
            rawmode += "w"
        raw = socket.SocketIO(self, rawmode)  # type: ignore[arg-type]
        self.socket._io_refs += 1  # type: ignore[attr-defined]
        if buffering is None:
            buffering = -1
        if buffering < 0:
            buffering = io.DEFAULT_BUFFER_SIZE
        if buffering == 0:
            if not binary:
                raise ValueError("unbuffered streams must be binary")
            return raw
        buffer: BinaryIO
        if reading and writing:
            buffer = io.BufferedRWPair(raw, raw,
                                       buffering)  # type: ignore[assignment]
        elif reading:
            buffer = io.BufferedReader(raw, buffering)
        else:
            assert writing
            buffer = io.BufferedWriter(raw, buffering)
        if binary:
            return buffer
        text = io.TextIOWrapper(buffer, encoding, errors, newline)
        text.mode = mode  # type: ignore[misc]
        return text
コード例 #23
0
def test_read_from_the_write_only_stream(loop, mock, socket_pair):
    write_only = socket.SocketIO(socket_pair[0], 'wb')

    def collapse(stream):
        try:
            stream.read(10)
        except io.UnsupportedOperation:
            mock()

    loop.add_read_stream(write_only, collapse)
    socket_pair[1].send(b"bar")
    loop.next_tick()
    mock.assert_called_once()
コード例 #24
0
    def __init__(self, host, port):
        self.socket = socket.socket()
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
        self.socket.connect((host, port))
        self.buffered_reader = io.BufferedReader(
            socket.SocketIO(self.socket, 'rb'))

        self.previous_players = None
        self.previous_player_by_id = {}
        self.previous_facilities = None
        self.previous_facility_by_id = {}
        self.terrain_by_cell_x_y = None
        self.weather_by_cell_x_y = None
コード例 #25
0
    def makefile(self,
                 mode="r",
                 buffering=None,
                 encoding=None,
                 errors=None,
                 newline=None):
        """
        Python's httpclient uses makefile and buffered io when reading HTTP
        messages and we need to support it.

        This is unfortunately a copy and paste of socket.py makefile with small
        changes to point to the socket directly.
        """
        if not set(mode) <= {"r", "w", "b"}:
            raise ValueError("invalid mode %r (only r, w, b allowed)" %
                             (mode, ))

        writing = "w" in mode
        reading = "r" in mode or not writing
        assert reading or writing
        binary = "b" in mode
        rawmode = ""
        if reading:
            rawmode += "r"
        if writing:
            rawmode += "w"
        raw = socket.SocketIO(self, rawmode)
        self.socket._io_refs += 1
        if buffering is None:
            buffering = -1
        if buffering < 0:
            buffering = io.DEFAULT_BUFFER_SIZE
        if buffering == 0:
            if not binary:
                raise ValueError("unbuffered streams must be binary")
            return raw
        if reading and writing:
            buffer = io.BufferedRWPair(raw, raw, buffering)
        elif reading:
            buffer = io.BufferedReader(raw, buffering)
        else:
            assert writing
            buffer = io.BufferedWriter(raw, buffering)
        if binary:
            return buffer
        text = io.TextIOWrapper(buffer, encoding, errors, newline)
        text.mode = mode
        return text
コード例 #26
0
 def makefile(self,
              mode="r",
              buffering=None,
              *,
              encoding=None,
              errors=None,
              newline=None):
     """makefile(...) -> an I/O stream connected to the socket
     The arguments are as for io.open() after the filename, except the only
     supported mode values are 'r' (default), 'w' and 'b'.
     """
     # XXX refactor to share code?
     if not set(mode) <= {"r", "w", "b"}:
         raise ValueError("invalid mode %r (only r, w, b allowed)" %
                          (mode, ))
     writing = "w" in mode
     reading = "r" in mode or not writing
     assert reading or writing
     binary = "b" in mode
     rawmode = ""
     if reading:
         rawmode += "r"
     if writing:
         rawmode += "w"
     raw = socket.SocketIO(self, rawmode)
     self._io_refs += 1
     if buffering is None:
         buffering = -1
     if buffering < 0:
         buffering = io.DEFAULT_BUFFER_SIZE
     if buffering == 0:
         if not binary:
             raise ValueError("unbuffered streams must be binary")
         return raw
     if reading and writing:
         buffer = io.BufferedRWPair(raw, raw, buffering)
     elif reading:
         buffer = io.BufferedReader(raw, buffering)
     else:
         assert writing
         buffer = io.BufferedWriter(raw, buffering)
     if binary:
         return buffer
     text = io.TextIOWrapper(buffer, encoding, errors, newline)
     text.mode = mode
     return text
コード例 #27
0
    def makefile(self, mode='r', bufsize=-1):
        """Create a file object for the TLS connection (socket emulation).

        @rtype: L{socket._fileobject}
        """
        self._refCount += 1
        # So, it is pretty fragile to be using Python internal objects
        # like this, but it is probably the best/easiest way to provide
        # matching behavior for socket emulation purposes.  The 'close'
        # argument is nice, its apparently a recent addition to this
        # class, so that when fileobject.close() gets called, it will
        # close() us, causing the refcount to be decremented (decrefAsync).
        #
        # If this is the last close() on the outstanding fileobjects / 
        # TLSConnection, then the "actual" close alerts will be sent,
        # socket closed, etc.
        if sys.version_info < (3,):
            return socket._fileobject(self, mode, bufsize, close=True)
        else:
            # XXX need to wrap this further if buffering is requested
            return socket.SocketIO(self, mode)
コード例 #28
0
 def connect_tcp_server(self,
                        interface='0.0.0.0',
                        port=16107,
                        timeout=None,
                        ssl_protocol=None,
                        verify_mode=ssl.CERT_OPTIONAL,
                        ca_cert=None,
                        certfile=None,
                        keyfile=None,
                        cert_pass=None):
     slog = getSyslog()
     listen_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     listen_s.bind((interface, port))
     listen_s.listen(1)
     slog.logsys('Waiting for connections')
     s, addr = listen_s.accept()
     if ssl_protocol == True:
         raw_sock = ssl.wrap_socket(s,
                                    keyfile=keyfile,
                                    certfile=certfile,
                                    cert_reqs=verify_mode,
                                    ca_certs=ca_cert,
                                    server_side=True)
         raw_sock.settimeout(10)
         self.ssl = True
     else:
         raw_sock = s
     if timeout != None: raw_sock.settimeout(timeout)
     else: raw_sock.settimeout(None)
     slog.logsys("Connected from", addr)
     if ssl_protocol == True:
         cert = raw_sock.getpeercert()
         slog.logsys('Peer cert', cert)
     self.__connection = socket.SocketIO(raw_sock, "rwb")
     self.__raw_socket = raw_sock
     self.__timeout = timeout
コード例 #29
0
def MakeFile_PY3(sock, mode='r', bufsize=io.DEFAULT_BUFFER_SIZE):
    if 'r' in mode:
        return io.BufferedReader(socket.SocketIO(sock, mode), bufsize)
    else:
        return BufferedWriter(socket.SocketIO(sock, mode), bufsize)
コード例 #30
0
    def https_open(self, req):
        # type: (Request) -> addinfourl
        """Return an addinfourl object for the request, using http_class.

        http_class must implement the HTTPConnection API from httplib.
        The addinfourl return value is a file-like object.  It also
        has methods and attributes including:

            - info(): return a mimetools.Message object for the headers

            - geturl(): return the original request URL

            - code: HTTP status code
        """
        # https://docs.python.org/3.3/library/urllib.request.html#urllib.request.Request.get_host
        try:  # up to python-3.2
            host = req.get_host()
        except AttributeError:  # from python-3.3
            host = req.host
        if not host:
            raise URLError('no host given')

        # Our change: Check to see if we're using a proxy.
        # Then create an appropriate ssl-aware connection.
        full_url = req.get_full_url()
        target_host = url_parse(full_url)[1]

        if target_host != host:
            request_uri = urldefrag(full_url)[0]
            h = httpslib.ProxyHTTPSConnection(host=host, ssl_context=self.ctx)
        else:
            try:  # up to python-3.2
                request_uri = req.get_selector()
            except AttributeError:  # from python-3.3
                request_uri = req.selector
            h = httpslib.HTTPSConnection(host=host, ssl_context=self.ctx)
        # End our change
        h.set_debuglevel(self._debuglevel)

        headers = dict(req.headers)
        headers.update(req.unredirected_hdrs)
        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        try:
            h.request(req.get_method(), request_uri, req.data, headers)
            r = h.getresponse()
        except socket.error as err:  # XXX what error?
            raise URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.

        # Wrap the HTTPResponse object in socket's file object adapter
        # for Windows.  That adapter calls recv(), so delegate recv()
        # to read().  This weird wrapping allows the returned object to
        # have readline() and readlines() methods.
        r.recv = r.read
        if six.PY2:
            fp = socket._fileobject(r, close=True)
        else:
            r._decref_socketios = lambda: None
            r.ssl = h.sock.ssl
            r._timeout = -1.0
            r.recv_into = r.readinto
            fp = socket.SocketIO(r, 'rb')

        resp = addinfourl(fp, r.msg, req.get_full_url())
        resp.code = r.status
        resp.msg = r.reason
        return resp