uav2-checkpoint.ipynb 1.85 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import socket \n",
    "import select \n",
    "import os "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def removeAllFile(filePath):\n",
    "    if os.path.exists(filePath):\n",
    "        for file in os.scandir(filePath):\n",
    "            os.remove(file.path)\n",
    "        return 'Remove All File'\n",
    "    else:\n",
    "        return 'Directory Not Found'\n",
    "\n",
    "print(removeAllFile('/home/pi/capdesign/frames'))\n",
    "address = ('192.168.51.1', 8083)\n",
    "sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n",
    "sock.bind(address)\n",
    "\n",
    "while True:\n",
    "    data, addr = sock.recvfrom(1024)\n",
    "    if data:\n",
    "        data = data.decode()\n",
    "        print(\"filename\", data)\n",
    "        file_name = data.strip()\n",
    "    \n",
    "    f = open('frames/{}'.format(file_name), 'wb')\n",
    "    \n",
    "    while True:\n",
    "        ready = select.select([sock], [], [], 3)\n",
    "        if ready[0]:\n",
    "            data, addr = sock.recvfrom(1024)\n",
    "            if(data == \"FINISH\".encode('utf-8')):\n",
    "                f.close()\n",
    "                break\n",
    "            else:\n",
    "                f.write(data)        "
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}