resource_server.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
00019 from os import path
00020 import subprocess
00021 import sys
00022 import threading
00023 import time
00024 
00025 import rospy
00026 
00027 default_img_path = '../res/pictures/'
00028 
00029 class MyHandler(BaseHTTPRequestHandler):            
00030     def do_GET(self):
00031         global default_img_path
00032         print "\nIncoming request!"
00033         try:
00034             spath = self.path[1:]
00035             if spath.endswith('.jpg') or spath.endswith('.jpeg'):
00036                 self.send_response(200)
00037                 self.send_header('Content-type','image/jpg')
00038                 self.end_headers()
00039                 fn = path.abspath(default_img_path+spath)
00040                 with open(fn, 'rb') as f:
00041                     self.wfile.write(f.read())
00042                 return
00043 
00044             elif spath.endswith('.png'):
00045                 self.send_response(200)
00046                 self.send_header('Content-type','image/png')
00047                 self.end_headers()
00048                 fn = path.abspath(default_img_path+spath)
00049                 print 'fn: ', fn
00050                 with open(fn, 'rb') as f:
00051                     self.wfile.write(f.read())
00052                 return
00053 
00054         except IOError:
00055             self.send_error(404, 'File Not Found: %s' % self.path)
00056 
00057 class ResourceServer(threading.Thread):
00058     def __init__(self):
00059         threading.Thread.__init__(self)
00060         self.server = None
00061         self.daemon = True
00062         self.ns_global_prefix="/android/resource_server"
00063         if rospy.has_param(self.ns_global_prefix + "/default_img_path"):
00064             global default_img_path
00065             default_img_path = rospy.get_param(self.ns_global_prefix + "/default_img_path")
00066             if not default_img_path.endswith("/"):
00067                 default_img_path = default_img_path + "/"
00068 
00069     def run(self):
00070         self.server = HTTPServer(('', 44644), MyHandler)
00071         print '\nStarted Android resource server on port 44644'
00072         self.server.serve_forever()
00073 
00074     def close(self):
00075         self.server.shutdown()
00076         self.server.socket.close()
00077 
00078 def resource_server_main():
00079     rospy.init_node('android_resource_server')
00080     resServer = ResourceServer()
00081     resServer.start()
00082     rospy.spin()
00083     resServer.close()


cob_android_resource_server
Author(s): Benjamin Maidel
autogenerated on Thu Jun 6 2019 21:46:24