ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

Easiest way to implement HTTP server that can send ROS messages

asked 2016-05-16 10:22:51 -0500

biellls gravatar image

I have a web interface that sends http requests to a server. When this server receives a request it needs to send messages through a ROS topic. Since this server will be very simple I thought that the easiest way would be to use Flask to make a REST service, but ROS doesn't seem to play nice with Flask. This is the code I am trying to execute:

#!/usr/bin/env python

## RESTful server that controls robot arm

import rospy
from std_msgs.msg import String
from flask import Flask, jsonify, request, abort

app = Flask(__name__)

def talker():
    rospy.loginfo('Starting talker')
    pub = rospy.Publisher(topic, String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rospy.loginfo('Talker started')

@App.route('/actions/execute', methods=['GET'])
def get_execute():
    action = request.args.get('action')
    if not action:
    return action

if __name__ == '__main__':
    rospy.loginfo("Starting Flask server")

If I comment out the line that calls talker() in main, the server starts correctly. If I execute it as shown however, I get the following:

[INFO] [WallTime: 1463411613.101553] Talker started
[INFO] [WallTime: 1463411613.102112] Starting Flask server
[INFO] [WallTime: 1463411613.576470] Talker started
[INFO] [WallTime: 1463411613.576941] Starting Flask server

And the Flask server never starts. I also can't kill the process with ctrl-c. Is there a way to make ROS work with Flask? And if there isn't, what is the easiest way to accomplish what I'm trying?

edit retag flag offensive close merge delete


I am currently working on the same concept, however am also having to success. I am not super familiar with Flask, however it seems that both Flask and Ros require threads for their nodes. Have you had any success after this post?

iarc_team_erau gravatar imageiarc_team_erau ( 2017-04-17 16:23:45 -0500 )edit

@iarc_team_erau as far as I know there is no way to use ROS in a python server. Look into rosbridge/roslibjs which let you do the same (it's what I ended up using )

biellls gravatar imagebiellls ( 2017-04-18 05:09:10 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-12-05 02:52:40 -0500

I think it can be made to work with rospy and Flask - the example below runs.

#! /usr/bin/env python

import os
import rospy
import threading

from flask import Flask

from std_msgs.msg import UInt32

def ros_callback(msg):

threading.Thread(target=lambda: rospy.init_node('example_node', disable_signals=True)).start()
rospy.Subscriber('/listener', UInt32, ros_callback)
pub = rospy.Publisher('/talker', UInt32, queue_size=10)

app = Flask(__name__)

def hello_world():
    msg = UInt32() = 1

    return 'Hello, World!'

if __name__ == '__main__':['ROS_IP'], port=3000)
edit flag offensive delete link more

answered 2017-12-29 22:30:54 -0500

Will_Welker gravatar image

It seems that rosbridge_server is what you are looking for.

Rosbridge server is part of the rosbridge_suite of packages, providing a WebSocket transport layer. A WebSocket is a low-latency, bidirectional communication layer between clients (web browsers) and servers. By providing a WebSocket connection, rosbridge server allows webpages to talk ROS using the rosbridge protocol.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-05-16 10:22:51 -0500

Seen: 1,758 times

Last updated: May 16 '16