Git Product home page Git Product logo

filesocket's Introduction

filesocket

The filesocket module implements socket behavior backed by two file-like object to and from which the actual data is read and written.

Create a filesocket wrapper using the FileSocket class.

>>> import sys
>>> from filesocket import FileSocket
>>> fsocket = FileSocket(None, sys.stdout)

By default, the socket uses sys.stdin for reading and sys.stdout for writing.

>>> fsocket.send('foo')
foo3
>>> fsocket.recv(0)
''

File sockets can also be given specific file objects to use.

>>> from StringIO import StringIO
>>> in_file = StringIO('bar')
>>> out_file = StringIO()
>>> fsocket = FileSocket(in_file, out_file)
>>> fsocket.send('foo')
3
>>> out_file.getvalue()
'foo'
>>> fsocket.recv(3)
'bar'

The socket can be shutdown. The how argument controls whether the in_file, out_file or both are closed.

>>> in_file.closed
False
>>> out_file.closed
False
>>> import socket
>>> fsocket.shutdown(socket.SHUT_RD)
>>> in_file.closed
True
>>> out_file.closed
False
>>> fsocket.shutdown(socket.SHUT_WR)
>>> in_file.closed
True
>>> out_file.closed
True
>>> in_file = StringIO('bar')
>>> out_file = StringIO()
>>> fsocket = FileSocket(in_file, out_file)
>>> in_file.closed
False
>>> out_file.closed
False
>>> fsocket.shutdown(socket.SHUT_RDWR)
>>> in_file.closed
True
>>> out_file.closed
True

The close() method just deletes the references to the files but doesn't necessarily close them.

>>> in_file = StringIO('bar')
>>> out_file = StringIO()
>>> fsocket = FileSocket(in_file, out_file)
>>> in_file.closed
False
>>> out_file.closed
False
>>> fsocket.in_file is in_file
True
>>> fsocket.out_file is out_file
True
>>> fsocket.close()
>>> in_file.closed
False
>>> out_file.closed
False
>>> hasattr(fsocket, 'in_file')
False
>>> hasattr(fsocket, 'out_file')
False

The fileno() method returns the file descriptor for the in_file by default.

>>> import tempfile
>>> in_file = tempfile.TemporaryFile()
>>> out_file = tempfile.TemporaryFile()
>>> fsocket = FileSocket(in_file, out_file)
>>> fsocket.fileno() == in_file.fileno()
True
>>> fsocket.fileno() == out_file.fileno()
False

If the use_out_fileno argument is True, then the descriptor of the out_file will be returned instead.

>>> fsocket = FileSocket(in_file, out_file, use_out_fileno=True)
>>> fsocket.fileno() == in_file.fileno()
False
>>> fsocket.fileno() == out_file.fileno()
True

The fileno method is only available if the specified file has a fileno attribute itself.

>>> in_file = StringIO('bar')
>>> out_file = StringIO()
>>> fsocket = FileSocket(in_file, out_file)
>>> hasattr(fsocket, 'fileno')
False
>>> fsocket = FileSocket(in_file, out_file, use_out_fileno=True)
>>> hasattr(fsocket, 'fileno')
False

As a slight optimization, underlying files' read() method is used directly for the file socket's recv() and methods.

>>> in_file = StringIO('bar')
>>> out_file = StringIO()
>>> fsocket = FileSocket(in_file, out_file)
>>> fsocket.recv.im_func is in_file.read.im_func
True
>>> fsocket.recv(3)
'bar'

FileSocket`s cannot support the `flags argument to recv.

>>> fsocket.recv(3, 0)
Traceback (most recent call last):
TypeError: read() takes at most 2 arguments (3 given)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.