|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# // -- BEGIN LICENSE BLOCK ---------------------------------------------- |
| 4 | +# // Copyright 2021 Universal Robots A/S |
| 5 | +# // |
| 6 | +# // Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# // you may not use this file except in compliance with the License. |
| 8 | +# // You may obtain a copy of the License at |
| 9 | +# // |
| 10 | +# // http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# // |
| 12 | +# // Unless required by applicable law or agreed to in writing, software |
| 13 | +# // distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# // See the License for the specific language governing permissions and |
| 16 | +# // limitations under the License. |
| 17 | +# // -- END LICENSE BLOCK ------------------------------------------------ |
| 18 | + |
| 19 | +# A simple python 3 server to test the External Control URCap |
| 20 | +# The server answer request from the URCap with the context of the given file |
| 21 | + |
| 22 | +import socket |
| 23 | +import socketserver |
| 24 | +import threading |
| 25 | +import argparse |
| 26 | + |
| 27 | +parser = argparse.ArgumentParser(description='Simple External Control server') |
| 28 | +parser.add_argument( |
| 29 | + "file", type=str, help="Path to the UR script file, which will be sent to the robot") |
| 30 | +parser.add_argument("-p", "--port", type=int, |
| 31 | + default=50002, help="Port number to use") |
| 32 | + |
| 33 | +args = parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
| 37 | + daemon_threads = True |
| 38 | + allow_reuse_address = True |
| 39 | + |
| 40 | + |
| 41 | +class FileHandler(socketserver.StreamRequestHandler): |
| 42 | + def handle(self): |
| 43 | + client = f'{self.client_address} on {threading.currentThread().getName()}' |
| 44 | + print(f'Connected: {client}') |
| 45 | + file = open(args.file, "r") |
| 46 | + while True: |
| 47 | + data = file.read() |
| 48 | + |
| 49 | + print(data) |
| 50 | + if not data: |
| 51 | + break |
| 52 | + self.wfile.write(data.encode('utf-8')) |
| 53 | + print(f'Closed: {client}') |
| 54 | + |
| 55 | + |
| 56 | +with ThreadedTCPServer(('', args.port), FileHandler) as server: |
| 57 | + print(f'The Simple External Control server is running on port', args.port) |
| 58 | + try: |
| 59 | + server.serve_forever() |
| 60 | + except KeyboardInterrupt: |
| 61 | + pass |
| 62 | + |
| 63 | + server.server_close() |
0 commit comments