Skip to content

Instantly share code, notes, and snippets.

@bofm
Last active July 26, 2016 13:25
Show Gist options
  • Select an option

  • Save bofm/8a34f5e8857d21a84c4e51799fc527f2 to your computer and use it in GitHub Desktop.

Select an option

Save bofm/8a34f5e8857d21a84c4e51799fc527f2 to your computer and use it in GitHub Desktop.
Python tarfile + socket
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import socket\n",
"import tarfile\n",
"import threading\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"! rm -rf /tmp/x\n",
"! mkdir /tmp/x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/private/tmp/x\n"
]
}
],
"source": [
"cd /tmp/x"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%bash\n",
"rm -rf 123 new_123\n",
"mkdir 123 new_123\n",
"echo 1 > 123/1\n",
"echo 2 > 123/2\n",
"echo 3 > 123/3"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def receive():\n",
" with socket.socket() as srecv:\n",
" srecv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n",
" srecv.bind(('', 9999))\n",
" srecv.listen(1)\n",
" conn, addr = srecv.accept()\n",
"\n",
" with conn.makefile('rb') as f:\n",
" with tarfile.open(mode='r|gz', fileobj=f) as tar:\n",
" tar.extractall('new_123/')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def send():\n",
" time.sleep(1)\n",
" with socket.socket() as ssend:\n",
" ssend.connect(('localhost', 9999))\n",
"\n",
" with ssend.makefile('wb') as f:\n",
" with tarfile.open(mode='w|gz', fileobj=f) as tar:\n",
" tar.add('123')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".\r\n",
"├── 123\r\n",
"│   ├── 1\r\n",
"│   ├── 2\r\n",
"│   └── 3\r\n",
"└── new_123\r\n",
"\r\n",
"2 directories, 3 files\r\n"
]
}
],
"source": [
"! tree"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"threading.Thread(target=receive, daemon=True).start()\n",
"send()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".\r\n",
"├── 123\r\n",
"│   ├── 1\r\n",
"│   ├── 2\r\n",
"│   └── 3\r\n",
"└── new_123\r\n",
" └── 123\r\n",
" ├── 1\r\n",
" ├── 2\r\n",
" └── 3\r\n",
"\r\n",
"3 directories, 6 files\r\n"
]
}
],
"source": [
"! tree"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment