Viraj Jagale
Viraj Jagale

Reputation: 74

return error messages on flask console as response

I have a flask app that manages iptables remotely. When I try to delete a rule that doesn't exist, I get following error on flask console:-

iptables: Bad rule (does a matching rule exist in that chain?).

whereas in response I only get this:-

Command '['iptables', '-t', 'filter', '-s', u'<some_ip>', '-j', u'DROP', '-D', u'INPUT']' returned non-zero exit status 1

This is how I have handled that exception:-

except subprocess.CalledProcessError as e:
            return "\n" + str(e) + "\n"

I want the error in flask console to be returned as response. How do I achieve that?

Upvotes: 0

Views: 550

Answers (1)

darvark
darvark

Reputation: 334

At first create command as string, then in subprocess use command.split().

cmd='iptables -t filter -s {}-j DROP -D INPUT'.format('127.0.0.1')

and i.e.

subprocess.check_output(cmd.split(), sterr=subprocess.STDOUT)

Second thing: why do you want to remove rule that does not exist?

Upvotes: 2

Related Questions