Convert curl command to BIG-IP Monitor Send String
Problem this snippet solves:
Convert curl commands into a HTTP or HTTPS monitor Send String.
How to use this snippet:
Save this code into a Python script and run as a replacement for curl.
Re...
Updated Jun 06, 2023
Version 3.0Eric_Chen
Employee
Joined May 16, 2013
Buddy_Edwards_1
Sep 15, 2017Nimbostratus
I'd like to say first that this is a great piece of code that has saved me a lot of headaches so thank you for creating it. The original script runs great in Python2 but had need for it to run natively in Python3 as the interpreter. This is a modified version that works in Python3.
Code
import getopt
import sys
from urllib.parse import splithost, splittype
optlist, args = getopt.getopt(sys.argv[1:], 'X:H:d:')
flat_optlist = dict(optlist)
method = flat_optlist.get('-X', 'GET')
(host, uri) = splithost(splittype(args[0])[1])
protocol = 'HTTP/1.1'
headers = ["%s %s %s" % (method, uri, protocol)]
headers.extend([h[1] for h in optlist if h[0] == '-H'])
if not fil`text`ter(lambda x: 'host:' in x.lower(), headers):
headers.insert(1, 'Host: %s' % (host))
send_string = "\\r\\n".join(headers)
send_string += "\\r\\n\\r\\n"
if '-d' in flat_optlist:
send_string += flat_optlist['-d'].replace('\n', '\\n')
send_string = send_string.replace("\"", "\\\"")
print("SEND STRING: " + send_string)