resource_server.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 from http.server import BaseHTTPRequestHandler, HTTPServer
19 from os import path
20 import subprocess
21 import sys
22 import threading
23 import time
24 
25 import rospy
26 
27 default_img_path = '../res/pictures/'
28 
29 class MyHandler(BaseHTTPRequestHandler):
30  def do_GET(self):
31  global default_img_path
32  print("\nIncoming request!")
33  try:
34  spath = self.path[1:]
35  if spath.endswith('.jpg') or spath.endswith('.jpeg'):
36  self.send_response(200)
37  self.send_header('Content-type','image/jpg')
38  self.end_headers()
39  fn = path.abspath(default_img_path+spath)
40  with open(fn, 'rb') as f:
41  self.wfile.write(f.read())
42  return
43 
44  elif spath.endswith('.png'):
45  self.send_response(200)
46  self.send_header('Content-type','image/png')
47  self.end_headers()
48  fn = path.abspath(default_img_path+spath)
49  print('fn: ', fn)
50  with open(fn, 'rb') as f:
51  self.wfile.write(f.read())
52  return
53 
54  except IOError:
55  self.send_error(404, 'File Not Found: %s' % self.path)
56 
57 class ResourceServer(threading.Thread):
58  def __init__(self):
59  threading.Thread.__init__(self)
60  self.server = None
61  self.daemon = True
62  self.ns_global_prefix="/android/resource_server"
63  if rospy.has_param(self.ns_global_prefix + "/default_img_path"):
64  global default_img_path
65  default_img_path = rospy.get_param(self.ns_global_prefix + "/default_img_path", '')
66  if not default_img_path.endswith("/"):
67  default_img_path = default_img_path + "/"
68 
69  def run(self):
70  self.server = HTTPServer(('', 44644), MyHandler)
71  print('\nStarted Android resource server on port 44644')
72  self.server.serve_forever()
73 
74  def close(self):
75  self.server.shutdown()
76  self.server.socket.close()
77 
79  rospy.init_node('android_resource_server')
80  resServer = ResourceServer()
81  resServer.start()
82  rospy.spin()
83  resServer.close()


cob_android_resource_server
Author(s): Benjamin Maidel
autogenerated on Tue Jan 19 2021 03:49:31