Need to pass multiple variables to payload variables - python-3.x

I am trying to pass multiple variables to payload using format & +str(Var)+ but I am not getting the expected output. I have the hostnames in a file & get a password as input and want to pass it to the payload.
I am getting an error related to "Error while parsing JSON payload or an incompatible argument type for the requested resource"
for x in content:
url='https://url/a/b/c/{}'.format(x.strip())
payload=('{{"ip-address": "x.x.x.x","user-name": "john","password": "'+ str(Pass) +'","db-name": "'+ str(x.strip()) +'","service-name": "y","port": "y","connection-string": "y"}}')
response = req.post(url,json=payload,headers=add_cookie,verify=False)
======================
for x in content:
url='https://url/a/b/c/{}'.format(x.strip())
payload={"ip-address": "x.x.x.x","user-name": "john","password": "{}","db-name": "{}","service-name": "y","port": "y","connection-string": "y"}.format(Pass, x.strip())
response = req.post(url,json=payload,headers=add_cookie,verify=False)

In first part your payload is a string and not a dict, it should be
payload={"ip-address": "x.x.x.x","user-name": "john","password": str(Pass),"db-name": str(x.strip()),"service-name": "y","port": "y","connection-string": "y"}
In the second one you're using the format function on a dict type which is wrong.

Related

error trying to recreate php's dechex function in python3

i have a php file that takes a simple 8 digit id and converts it to hex using
dechex(intval($id))
i am now trying todo the same thing in python i start by grabbing my list of ids from the web these are returned as strings such as
00274956 , 00002645, 00000217
i then convert them to intagers and hex them using
hex(int(item_id))
but i am getting the error
ValueError: invalid literal for int() with base 10: 'init'
here is the code the id comes direct from a http get request
FILE_NUMBER = int(ITEM_ID)
FILE_HEX = hex(FILE_NUMBER)
FILE_NEW = FILE_HEX + ".pdf"

how to patch configmap field using python client library

I have below configmap.yml i want to patch/update date field from python script from container in kubernates deployment i searched various side but couldn't get any reference to do that. Any reference or code sample would be a great help
apiVersion: v1
kind: ConfigMap
metadata:
name: sample-configmap
labels:
app: test
parameter-type: sample
data:
storage.ini: |
[DateInfo]
date=1970-01-01T00:00:00.01Z
I went through this reference code but couldn't figure out what will be content of body and which parameter i should use and which parameter i should neglect
partially update the specified ConfigMap
from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest
import ApiException
from pprint import pprint
configuration = kubernetes.client.Configuration()
# Configure API key authorization: BearerToken configuration.api_key['authorization'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'
# Defining host is optional and default to http://localhost configuration.host = "http://localhost"
# Enter a context with an instance of the API kubernetes.client
with kubernetes.client.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = kubernetes.client.CoreV1Api(api_client)
name = 'name_example' # str | name of the ConfigMap
namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
body = None # object |
pretty = 'pretty_example'
dry_run = 'dry_run_example'
field_manager = 'field_manager_example'
force = True
try:
api_response = api_instance.patch_namespaced_config_map(name, namespace, body, pretty=pretty, dry_run=dry_run, field_manager=field_manager, force=force)
pprint(api_response)
except ApiException as e:
print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
The body parameter in patch_namespaced_config_map is the actual configmap data that you want to patch and needs to be first obtained with read_namespaced_config_map.
Following steps are required for all the operations that have the body argument:
Get the data using the read_*/get_*method
Use the data returned in the first step in the API modifying the object.
Further, for most cases, it is enough to pass the required arguments namely
name, namespace and body but here is the info about each:
Parameters
Name
Type
Description
Notes
name
str
name of the ConfigMap
namespace
str
object name and auth scope, such as for teams and projects
body
object
pretty
str
If 'true', then the output is pretty printed.
[optional]
dry_run
str
When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
[optional]
field_manager
str
fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch).
[optional]
force
bool
Force is going to "force" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests.
[optional]
Review the K8s python client README for the list of all supported APIs and their usage.

Invoking sagemaker endpoint using AWS-Lamda throwing parameter validation error

I trained an ML model in AWS Sagemaker and created an endpoint. I want to invoke it using AWS-Lambda. My model has 30 predictor variables. So I passed them into test event of Lambda as dict type as mentioned below
{
"Time": "10 ",
"V1": "1.449043781 ",
"V2": "-1.176338825 ",
"V3": "0.913859833 ",
"V4": "-1.375666655 ",
"V5": "-1.971383165 ",
"V6": "-0.629152139 ",
"V7": "-1.423235601 ",
"V8": "0.048455888 ",
"V9": "-1.720408393 ",
"V10": "1.626659058 ",
"V11": "1.19964395 ",
"V12": "-0.671439778 ",
"V13": "-0.513947153 ",
"V14": "-0.095045045 ",
"V15": "0.230930409 ",
"V16": "0.031967467 ",
"V17": "0.253414716 ",
"V18": "0.854343814 ",
"V19": "-0.221365414 ",
"V20": "-0.387226474 ",
"V21": "-0.009301897 ",
"V22": "0.313894411 ",
"V23": "0.027740158 ",
"V24": "0.500512287 ",
"V25": "0.251367359 ",
"V26": "-0.129477954 ",
"V27": "0.042849871 ",
"V28": "0.016253262 ",
"Amount": "7.8"
}
Now I ran below mentioned code in AWS Lambda
import json
import os
import csv
import boto3
import io
import codecs
endpoint_name = os.environ['ENDPOINT_NAME']
runtime = boto3.client('runtime.sagemaker')
def lambda_handler(event, context):
print("received event: "+json.dumps(event,indent=2))
data = json.loads(json.dumps(event))
payload = data["Time"]+data["V1"]+data["V2"]+data["V3"]+data["V4"]+data["V5"]+data["V6"]+data["V7"]+data["V8"]+data["V9"]+data["V10"]+data["V11"]+data["V12"]+data["V13"]+data["V14"]+data["V15"]+data["V16"]+data["V17"]+data["V18"]+data["V19"]+data["V20"]+data["V21"]+data["V22"]+data["V23"]+data["V24"]+data["V25"]+data["V26"]+data["V27"]+data["V28"]+data["Amount"]
payload = payload.split(" ")
payload = [codecs.encode(i,'utf-8') for i in payload]
payload=[bytearray(i) for i in payload]
print(payload)
response = runtime.invoke_endpoint(EndpointName=endpoint_name,ContentType='text/csv',Body=payload)
print(response)
result=json.loads(response['Body'].decode())
pred = int(float(response))
predicted_label = 'fraud' if pred==1 else 'not fraud'
return predicted_label
This code is throwing below this error
[ERROR] ParamValidationError: Parameter validation failed:
Invalid type for parameter Body, value: [bytearray(b'10'), bytearray(b'1.449043781'), bytearray(b'-1.176338825'), bytearray(b'0.913859833'), bytearray(b'-1.375666655'), bytearray(b'-1.971383165'), bytearray(b'-0.629152139'), bytearray(b'-1.423235601'), bytearray(b'0.048455888'), bytearray(b'-1.720408393'), bytearray(b'1.626659058'), bytearray(b'1.19964395'), bytearray(b'-0.671439778'), bytearray(b'-0.513947153'), bytearray(b'-0.095045045'), bytearray(b'0.230930409'), bytearray(b'0.031967467'), bytearray(b'0.253414716'), bytearray(b'0.854343814'), bytearray(b'-0.221365414'), bytearray(b'-0.387226474'), bytearray(b'-0.009301897'), bytearray(b'0.313894411'), bytearray(b'0.027740158'), bytearray(b'0.500512287'), bytearray(b'0.251367359'), bytearray(b'-0.129477954'), bytearray(b'0.042849871'), bytearray(b'0.016253262'), bytearray(b'7.8')], type: <class 'list'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object
I understand that somehow I need to pass my 30 features into Lambda function such that data type of payload is compatible with ContentType for respnse to work. Can someone please explain how to do it?
edit: I'm trying this problem by looking at this aws blog. I don't quite understand how the author of above mentioned blog did it.
Pretty sure your error is coming from:
response = runtime.invoke_endpoint(EndpointName=endpoint_name,ContentType='text/csv',Body=payload)
but just FYI, it's very helpful if when you ask the a question about an exception, you point out the line causing it :)
The invoke_endpoint() docs specify that Body has to be:
bytes or seekable file-like object
and that Body
provides input data, in the format specified in the ContentType request header.
Your content type is text/csv so the endpoint will be expecting bytes which represent a csv string. The question then is what is it getting instead that's making it unhappy?
payload = data["Time"]+data["V1"]+ ...
At this point, payload is a string
payload = payload.split(" ")
Now it's a list of strings
payload = [codecs.encode(i,'utf-8') for i in payload]
Now it's a list of strings encoded in utf-8 (this step might be unnecessary)
payload=[bytearray(i) for i in payload]
Now it's a list of bytearrays.
And that list is what you're passing as Body.
So, to get it to work, you'll need to change your logic to:
Make your original input into a CSV
Convert that CSV into one single byte array
Pass that array as part of your invocation
A note on style: passing the input numbers as strings, with trailing spaces so you can split(" ") is pretty hacky. Just pass the input as plain numbers (remove the " surrounding them), and find a way to do what you need with appending then splitting (these are effectively inverse operations, they cancel each other out).

Jmeter PostProcessor with Groovy and fetching content of response data

In JSR223 PostProcessor I am using this method to get the response data:
def json = new JsonSlurper().parseText(response)
Here is a snippet of my json output is like this
XmlItemResult:[[xPath:/Blocks/Block[001], name:abc, folder:\A\abc\a1, id:84, information:[[xPath:/Blocks/Block[001], result:Block number 1: abc], [xPath:/Blocks/Block[001]/Steps/CallSteps/Step[001], result:Call step StepNo 1],
folder:\Voice133, id:2542, information:[[xPath:/TestCases/TestCase[001],
This response, as you see contains two things which I am interested in:
folder:\A\abc\a1, id:84,
folder:\Voice133, id:2542,
I need to get the id value for only this line --> folder:\Voice133, id:2542,
note 2542 is variable and can be different each time and after each run.
I tried
json.find ("Voice133, id:(.+?),")
Your string is not a valid JSON, you can check it yourself using any online JSON validator, therefore you won't be able to use JsonSlurper, you will have to go for Regular Expressions instead.
In Groovy you can use =~ - Find Operator in order to be able to extract the required value(s), example code would be something like:
def response = 'XmlItemResult:[[xPath:/Blocks/Block[001], name:abc, folder:\\A\\abc\\a1, id:84,' +
' information:[[xPath:/Blocks/Block[001], result:Block number 1: abc],' +
' [xPath:/Blocks/Block[001]/Steps/CallSteps/Step[001], result:Call step StepNo 1], '
def matcher = (response =~ 'folder:\\\\A\\\\abc\\\\a1, id:(\\d+),')
if (matcher.find()) {
log.info('Folder ID = ' + matcher.group(1))
}
Demo:
More information: Apache Groovy - Why and How You Should Use It

Can't get HTTP headers from request

I'm trying to get the headers from a HTTP request. I've tried echoing them, only to get a compile error. I've tried looping through the headers as well. My code is:
var packet = newMultipartData()
packet["username"] = username
packet["password"] = password
var response = post(BASE & "users/login", multipart=packet)
echo response.headers
And the error I'm getting is: Error: type mismatch: got (HttpHeaders)
Figured it out - Turns out I can call the one I want from the list. Ex: response.headers["X-CSRF-TOKEN"]
The reason your code does not work because there is no $ proc defined for HttpHeaders:
let headers = newHttpHeaders()
headers["foo"] = "bar"
headers["foo2"] = "bar2"
echo headers["foo"] # compiles
echo headers # does _not_ compile
We can quickly implement an own $ proc ourselves:
proc `$`(h: HttpHeaders): string =
result = "{"
for key, value in h:
if result.len > 1: result.add(", ")
result.add(key)
result.add(": ")
result.add(value)
result.add("}")
The implementation is based on https://github.com/nim-lang/Nim/blob/master/lib/pure/collections/tables.nim#L333. Because HttpHeaders is just a type to a table, we can simply reuse the existing proc.
type
HttpHeaders* = ref object
table*: TableRef[string, seq[string]]

Resources