├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── pearsend.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sumit Ghosh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | # Include the license file 2 | include LICENSE 3 | include README.rst 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pearsend 2 | A simple CLI client for peer-to-peer file or message sending. Written in Python. 3 | 4 | [![PyPI version](https://badge.fury.io/py/pearsend.svg)](https://badge.fury.io/py/pearsend) 5 | 6 | ## Features 7 | 8 | - It supports file or message of size upto about 8.85 PeB (1 PiB ~ 10^6 GiB)!. 9 | - Protection against transmission error using `CRC32` checksum. 10 | - Comes with CLI (command-line argument) and Interactive mode, both! 11 | 12 | # Installation 13 | 14 | Install it using `pip` 15 | ```console 16 | $ pip3 install pearsend 17 | ``` 18 | 19 | # Usage Examples 20 | 21 | ## Command-line Mode 22 | 23 | __Help Text__ 24 | ```console 25 | sumit@HAL9000:~$ pearsend -h 26 | usage: pearsend [-h] [-i] [-f FILEPATH] [--host HOST] [-p PORT] 27 | [-m MESSAGE] 28 | {send,receive} 29 | 30 | positional arguments: 31 | {send,receive} Whether to send or receive 32 | 33 | optional arguments: 34 | -h, --help show this help message and exit 35 | -i, --interactive If the program is to be run in interactive mode 36 | -f FILEPATH, --filepath FILEPATH 37 | Path of the file to be sent or to save incoming data 38 | to 39 | --host HOST Address of the source or target machine 40 | -p PORT, --port PORT Port for listening on or sending to 41 | -m MESSAGE, --message MESSAGE 42 | Message to send 43 | ``` 44 | 45 | ### Sending text message 46 | 47 | __Receiver__ 48 | ```console 49 | sumit@HAL9000:~$ pearsend receive -p 5000 50 | [*] Listening for connections on: 10.194.52.135:5000 51 | 52 | [*] Connection from : 10.194.52.135:47804 53 | [*] The incoming data is > 54 | b'Hello HAL!' 55 | ``` 56 | 57 | __Sender__ 58 | ```console 59 | sumit@HAL9000:~$ pearsend send --host 10.194.52.135 -m "Hello HAL!" 60 | 61 | [*] Sent message succesfully! 62 | ``` 63 | 64 | ### Sending binary file 65 | 66 | __Receiver__ 67 | ```console 68 | sumit@HAL9000:~$ pearsend receive -p 5000 -f recd.png 69 | [*] Listening for connections on: 10.194.52.135:5000 70 | 71 | [*] Connection from : 10.194.52.135:47808 72 | [*] Incoming data saved to recd.png 73 | ``` 74 | 75 | __Sender__ 76 | ```console 77 | sumit@HAL9000:~$ pearsend send --host 10.194.52.135 -f image.png 78 | 79 | [*] Sent message succesfully! 80 | ``` 81 | 82 | 83 | ## Interactive Mode 84 | 85 | ### Sending text message 86 | 87 | __Receiver__ 88 | ```console 89 | sumit@HAL9000:~$ pearsend receive -i 90 | [?] Port to listen on: 91 | [?] File to save the incoming data to. Leave blank to output to terminal: 92 | [*] Listening for connections on: 10.194.52.135:5000 93 | 94 | [*] Connection from : 10.194.52.135:36240 95 | [*] The incoming data is > 96 | b'Hello HAL!' 97 | ``` 98 | 99 | __Sender__ 100 | ```console 101 | sumit@HAL9000:~$ pearsend send -i 102 | [?] The address of the target machine: 10.194.52.135 103 | [?] Enter the port to connect to: 104 | [?] The file to send. Leave blank for text message: 105 | [?] Enter the message: Hello HAL! 106 | 107 | [*] Sent message succesfully! 108 | ``` 109 | 110 | ### Sending binary file 111 | 112 | __Receiver__ 113 | ```console 114 | sumit@HAL9000:~$ pearsend receive -i 115 | [?] Port to listen on: 116 | [?] File to save the incoming data to. Leave blank to output to terminal: recd.jpg 117 | [*] Listening for connections on: 10.194.52.135:5000 118 | 119 | [*] Connection from : 10.194.52.135:36242 120 | [*] Incoming data saved to recd.jpg 121 | ``` 122 | 123 | __Sender__ 124 | ```console 125 | sumit@HAL9000:~$ pearsend send -i 126 | [?] The address of the target machine: 10.194.52.135 127 | [?] Enter the port to connect to: 128 | [?] The file to send. Leave blank for text message: image.jpg 129 | 130 | [*] Sent message succesfully! 131 | ``` 132 | -------------------------------------------------------------------------------- /pearsend.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import zlib 3 | import argparse 4 | 5 | 6 | def getdata(message): 7 | length = len(message) 8 | 9 | if len(str(length)) > 16: 10 | raise Exception('The message is too long! Exiting') 11 | 12 | data = '{:>16}'.format(str(length)).encode('UTF-8') + '{:>10}'.format(str(zlib.crc32(message))).encode('UTF-8') + message 13 | return data 14 | 15 | 16 | def send(host, port, message): 17 | data = getdata(message) 18 | 19 | sckt = socket.socket() 20 | sckt.connect((host, port)) 21 | 22 | totalsent = 0 23 | while totalsent < len(data): 24 | sent = sckt.send(data[totalsent:]) 25 | if not sent: 26 | raise RuntimeError('Socket connection broken!') 27 | totalsent = totalsent + sent 28 | 29 | 30 | def get_ip(): 31 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 32 | try: 33 | s.connect(('10.255.255.255', 1)) 34 | IP = s.getsockname()[0] 35 | except: 36 | IP = '127.0.0.1' 37 | finally: 38 | s.close() 39 | return IP 40 | 41 | 42 | def considerate_print(text=None, quiet=False): 43 | if not quiet: 44 | if not text: 45 | print() 46 | else: 47 | print(text) 48 | 49 | 50 | def receive(host, port, quiet=False): 51 | considerate_print('[*] Listening for connections on: {host}:{port}'.format(host=host, port=port), quiet) 52 | 53 | sckt = socket.socket() 54 | sckt.bind((host, port)) 55 | 56 | sckt.listen(1) 57 | conn, addr = sckt.accept() 58 | considerate_print(quiet=quiet) 59 | considerate_print('[*] Connection from : {addr[0]}:{addr[1]}'.format(addr=addr), quiet) 60 | 61 | chunks = [] 62 | bytes_received = 0 63 | chunk = conn.recv(16) 64 | length = int(chunk.decode('UTF-8')) 65 | 66 | checksum = int(conn.recv(10).decode('UTF-8')) 67 | 68 | while bytes_received < length: 69 | chunk = conn.recv(min(length-bytes_received, 1024)) 70 | if not chunk: 71 | raise RuntimeError('Socket connection broken!') 72 | chunks.append(chunk) 73 | bytes_received = bytes_received + len(chunk) 74 | 75 | data = b''.join(chunks) 76 | if (zlib.crc32(data) != checksum): 77 | raise RuntimeError("Checksums don't match!") 78 | return data 79 | 80 | 81 | def main(): 82 | parser = argparse.ArgumentParser() 83 | parser.add_argument('mode', help='Whether to send or receive', type=str, choices=['send', 'receive']) 84 | parser.add_argument('-i', '--interactive', help='If the program is to be run in interactive mode', action='store_true') 85 | parser.add_argument('-f', '--filepath', help='Path of the file to be sent or to save incoming data to', type=str) 86 | parser.add_argument('--host', help='Address of the source or target machine', type=str) 87 | parser.add_argument('-p', '--port', help='Port for listening on or sending to', type=int, default=5000) 88 | parser.add_argument('-m', '--message', help='Message to send', type=str) 89 | parser.add_argument('-q', '--quiet', help='Quiet mode', action='store_true') 90 | args = parser.parse_args() 91 | 92 | if args.mode=='send': 93 | if args.interactive: 94 | host = input('[?] The address of the target machine: ') 95 | port = int(input('[?] Enter the port to connect to: ') or '5000') 96 | filename = input('[?] The file to send. Leave blank for text message: ') 97 | else: 98 | host = args.host 99 | port = int(args.port or '5000') 100 | filename = args.filepath 101 | 102 | if filename: 103 | with open(filename, 'rb') as f: 104 | message = f.read() 105 | else: 106 | message = args.message.encode('UTF-8') 107 | if not message: 108 | message = input('[?] Enter the message: ').encode('UTF-8') 109 | 110 | send(host, port, message) 111 | considerate_print(quiet=args.quiet) 112 | considerate_print('[*] Sent message succesfully!', args.quiet) 113 | 114 | elif args.mode=='receive': 115 | if args.interactive: 116 | port = int(input('[?] Port to listen on: ') or '5000') 117 | destination = input('[?] File to save the incoming data to. Leave blank to output to terminal: ') 118 | else: 119 | port = args.port or '5000' 120 | destination = args.filepath 121 | 122 | try: 123 | message = receive(get_ip(), port, args.quiet) 124 | except RuntimeError as e: 125 | considerate_print('[!] RuntimeError: {}'.format(e), args.quiet) 126 | sys.exit(1) 127 | 128 | if destination: 129 | with open(destination, 'wb') as f: 130 | f.write(message) 131 | considerate_print('[*] Incoming data saved to {}'.format(destination), args.quiet) 132 | else: 133 | considerate_print('[*] The incoming data is > ', args.quiet) 134 | print(message) 135 | 136 | 137 | if __name__=='__main__': 138 | main() 139 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bdist_wheel] 2 | # This flag says that the code is written to work on both Python 2 and Python 3 | # 3. If at all possible, it is good practice to do this. If you cannot, you 4 | # will need to generate wheels for each Python version that you support. 5 | universal=1 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """A setuptools based setup module. 2 | 3 | See: 4 | https://packaging.python.org/en/latest/distributing.html 5 | https://github.com/pypa/sampleproject 6 | """ 7 | 8 | # Always prefer setuptools over distutils 9 | from setuptools import setup, find_packages 10 | # To use a consistent encoding 11 | from codecs import open 12 | from os import path 13 | 14 | here = path.abspath(path.dirname(__file__)) 15 | 16 | # Get the long description from the README file 17 | with open(path.join(here, 'README.rst'), encoding='utf-8') as f: 18 | long_description = f.read() 19 | 20 | setup( 21 | name='pearsend', 22 | 23 | # Versions should comply with PEP440. For a discussion on single-sourcing 24 | # the version across setup.py and the project code, see 25 | # https://packaging.python.org/en/latest/single_source_version.html 26 | version='0.0.3', 27 | 28 | description='A simple CLI client for peer-to-peer file or message sending', 29 | long_description=long_description, 30 | 31 | # The project's main homepage. 32 | url='https://github.com/SkullTech/PearSend', 33 | 34 | # Author details 35 | author='Sumit Ghosh', 36 | author_email='sumit@sumit-ghosh.com', 37 | 38 | # Choose your license 39 | license='MIT', 40 | 41 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 42 | classifiers=[ 43 | # How mature is this project? Common values are 44 | # 3 - Alpha 45 | # 4 - Beta 46 | # 5 - Production/Stable 47 | 'Development Status :: 4 - Beta', 48 | 49 | # Environment 50 | 'Environment :: Console', 51 | 52 | # Indicate who your project is intended for 53 | 'Intended Audience :: End Users/Desktop', 54 | 'Topic :: Communications', 55 | 'Topic :: Communications :: Chat', 56 | 'Topic :: Communications :: File Sharing', 57 | 'Topic :: Utilities', 58 | 59 | # Pick your license as you wish (should match "license" above) 60 | 'License :: OSI Approved :: MIT License', 61 | 62 | # Operating System 63 | 'Operating System :: OS Independent', 64 | 65 | 66 | # Specify the Python versions you support here. In particular, ensure 67 | # that you indicate whether you support Python 2, Python 3 or both. 68 | 'Programming Language :: Python :: 3', 69 | 'Programming Language :: Python :: 3.3', 70 | 'Programming Language :: Python :: 3.4', 71 | 'Programming Language :: Python :: 3.5', 72 | 'Programming Language :: Python :: 3.6', 73 | ], 74 | 75 | # What does your project relate to? 76 | keywords='sample setuptools development', 77 | 78 | # You can just specify the packages manually here if your project is 79 | # simple. Or you can use find_packages(). 80 | # packages=find_packages(exclude=['contrib', 'docs', 'tests']), 81 | 82 | # Alternatively, if you want to distribute just a my_module.py, uncomment 83 | # this: 84 | py_modules=["pearsend"], 85 | 86 | # List run-time dependencies here. These will be installed by pip when 87 | # your project is installed. For an analysis of "install_requires" vs pip's 88 | # requirements files see: 89 | # https://packaging.python.org/en/latest/requirements.html 90 | install_requires=[], 91 | 92 | # List additional groups of dependencies here (e.g. development 93 | # dependencies). You can install these using the following syntax, 94 | # for example: 95 | # $ pip install -e .[dev,test] 96 | # extras_require={ 97 | # 'dev': ['check-manifest'], 98 | # 'test': ['coverage'], 99 | # }, 100 | 101 | # If there are data files included in your packages that need to be 102 | # installed, specify them here. If using Python 2.6 or less, then these 103 | # have to be included in MANIFEST.in as well. 104 | # package_data={ 105 | # 'sample': ['package_data.dat'], 106 | # }, 107 | 108 | # Although 'package_data' is the preferred approach, in some case you may 109 | # need to place data files outside of your packages. See: 110 | # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa 111 | # In this case, 'data_file' will be installed into '/my_data' 112 | # data_files=[('my_data', ['data/data_file'])], 113 | 114 | # To provide executable scripts, use entry points in preference to the 115 | # "scripts" keyword. Entry points provide cross-platform support and allow 116 | # pip to create the appropriate form of executable for the target platform. 117 | entry_points={ 118 | 'console_scripts': [ 119 | 'pearsend=pearsend:main', 120 | ], 121 | }, 122 | ) 123 | --------------------------------------------------------------------------------