pycurl: How to authenticate making a POST request - python-2.7

Using the following command via cURL (Windows): curl -u username:password "https://website.com/update/" --data "simChangesList=%5B%7B%22simId%22%3A760590802%2C%22changeType%22%3A2%2C%22targetValue%22%3A%22000307%22%2C%22effectiveDate%22%3Anull%7D%5D" --compressed
I am able to successfully post my data authenticated as per the -u switch.
How can I successfully create the same POST request using pycurl?
import urllib
import pycurl
enc = urllib.quote('simChangesList:[{"simId":760590802,"changeType":2,"targetValue":000307,"effectiveDate":null}]')
c = pycurl.Curl()
c.setopt(c.URL, 'https://website.com/update/')
c.setopt(c.POSTFIELDS, enc)
c.setopt(c.VERBOSE, True)
c.perform()
c.close()
Executing the above code will not POST the data I am wanting it to do, and I suspect it's because I'm requiring authentication, as per the Windows cURL version.
How can I successfully modify my code so that I can POST my data authenticated?

Related

How to get the file name from a simple curl post request in aws lambda throu aws API gateway

I have the following curl request,
curl -X POST "https://foo123.execute-api.us-east-1.amazonaws.com/default/test-2" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=#wiki.png;type=image/png"
Due to the existing pipeline structure, I am not able to change the present architecture of this curl request, I want to access the name of this file in the lambda function when I am using aws API gateway as the trigger.
I am aware I can simply send the file name in header but that doesn't work for the pipeline I am going to use it with, so my question is.
How can I access the name of this file inside a lambda function in aws, given that I can't change my curl request.
To further give clarity to my question in flask APIs we are able to get the name of this uploaded file by simply using something like.
args = upload_parser.parse_args()
uploaded_file = args['file']
required_file_name = uploaded_file.filename
Interesting problem, actually you get the values of the body as a base64 encoded image you will have to either use any of the existing libraries to parse it for the name and content from body or write your own parser, your can refer to the code snippet below for reference :
import cv2
import os
import base64
import numpy as np
import email
def http_api(event):
post_data = base64.b64decode(event['body'])
# fetching content-type
try:
content_type = event["headers"]['Content-Type']
except:
content_type = event["headers"]['content-type']
# concate Content-Type: with content_type from event
ct = "Content-Type: "+content_type+"\n"
# parsing message from bytes
msg = email.message_from_bytes(ct.encode()+post_data)
if msg.is_multipart():
multipart_content = {}
# retrieving form-data
for part in msg.get_payload():
# checking if filename exist as a part of content-disposition header
if part.get_filename():
# fetching the filename
file_name = part.get_filename()
multipart_content[part.get_param('name', header='content-disposition')] = part.get_payload(decode=True)
img_str = multipart_content["file"]
nparr = np.fromstring(img_str, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return image

How to publish to a GCP pub/sub topic using HTTP Bridge in Python3 & CURL?

I am trying to publish to a pub/sub topic via HTTP Bridge using python3 & CURL.
**Python3**
import json
import logging
import os
import socket
import sys
import time
import requests
URL = 'https://cloudiotdevice.googleapis.com/v1/projects/{}/locations/{}/registries/{}/devices/{}:publishEvent'
JWT = 'JWT'
def main():
if not URL or not JWT:
sys.exit("Are the Environment Variables set?")
get_sensor_data(socket.gethostname())
def get_sensor_data(device_id):
while True:
print("in get_sensor data")
payload = {'device': str('asd'),
'type': str('adssaff'),
'timestamp': str(time.time()),
'data': json.dumps({'temperature': str('23'),
'humidity': str('442')})}
post_data(payload)
print("data printed")
time.sleep(5)
def post_data(payload):
payload = json.dumps(payload)
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': JWT
}
try:
req = requests.post(URL, json=str(payload), headers=headers)
print("request Successfull "+str(req))
except requests.exceptions.ConnectionError:
logging.error('Error posting data to Cloud Function!')
except requests.exceptions.MissingSchema:
logging.error('Error posting data to Cloud Function! Are Environment Variables set?')
if __name__ == '__main__':
This is giving an error 400 because i think i havent described the subfolder.
Now i am confuse that where can i define the subfolder(Topic name) in my code?
and is there only subfolder is missing? or i am doing something else wrong too?
CURL
i also tried using the CURL command described in
https://cloud.google.com/iot/docs/how-tos/http-bridge
The command is
curl -X POST -H 'authorization: Bearer JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "sub_folder": "SUBFOLDER"}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}:publishEvent'
It triggers my cloud function which means the authorization works but i am not able to see "DATA" in my logs. which i assume i am not providing the right format for the binary_data. whyt would be the right format if i would like to publish 'payload' described above using curl too?
It looks like you are using a JSON payload with the data field set to an object, rather than binary string. Try to either json.dumps the object in the 'data' field or send the 'data' field as a string.
From this documentation.
https://cloud.google.com/iot/docs/reference/cloudiotdevice/rest/v1/projects.locations.registries.devices/publishEvent
I found out that my payload request body was not correct.
so payload should look like this below ..
s= json.jumps('json object')
payload = {"subFolder": 'Sub_FOLDER_NAME', "binaryData": base64.b64encode(s.encode('utf-8'))}

how to know table to refrence when creating json web token in django

Am trying to implement json wen token in Django. I have been following tutorial from this source.link I have installed and configured all the necessary requirements.
To obtain users token here is the curl example
$ curl --request POST \
--url http://localhost:8000/jwt-auth/ \
--header 'content-type: application/json' \
--data '{"username": "myusername", "password": "mypass"}'
{"token": "YOUR_JWT_TOKEN"}
Here is my code for making call
import requests
item = { "username": "admin", "password": "admin"}
resp1 = requests.post("http://localhost:8000/auth-jwt/",
data=json.dumps(item),
headers={
"Content-Type":"application/json",
"Accept": "application/json"
}
)
print (resp1.status_code)
#print (resp1.content)
content = json.loads(resp1.content)
print(content)
#print(content['status'])
My issues:
When I run the above code, it returns error
non field error: unable to login with provided credentials
What does this mean. which table do I have to create to use their credentials. Currently I have users nancy on auth_user table.
Consequently, This similar issues has been resolved here but I cannot get it to work Stackoverflow link
Can someone tell me what that error above is all about and next step in solving the problem. Thanks
I found out my problem. I need to create a superuser account as per
$ python manage.py createsuperuser
so running this syntax squentially at command prompts solves my problem.
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Convert curl get command to urllib get request

I have a curl request that I want to convert to urllib in python2.
curl which works and gives son response:
curl -i -X GET -H "X-AUTH-TOKEN: $AUTH_TOKEN" \
-H "Accept: application/json" \
"https://api.xyz.com/apiv1.2/reports/nodes?start_date=2014-04-01&end_date=2014-04-21"
I tried the following code and it keeps on redirecting me to login html page as response. How can I convert the above curl request to urllib?
headers = {"Content-Type": "application/json", "AUTH_TOKEN":'1234yyzxx'}
data = urllib.urlencode(values)
request = urllib2.Request(ENDPOINT + '?' + data, headers=headers)
response = urllib2.urlopen(request)
text = response.read()
print text
Found it using requests library.
import requests
import json
response = requests.get(ENDPOINT, headers=headers, params=values)
text = json.loads(response.text)
However, I wouldn't mind answers in all non-deprecated libraries (urllib, urllib2, urllib3 etc).
Which one is faster?

How to pass date arguments for requests.get command for API data?

I'm trying to get past weather data from API source. However I'm running into issues on how to pass the date arguments and specify dates I'm trying to get data for. I have been successful in using curl GET command, but not been successful in translating that over to requests.get command.
Here's what works:
curl -X GET -G "http://url.com/lat/long" -d start="2018-04-01" -d end="2018-04-04" --user abcd:1234
Using subprocess command I have gotten that code to work.Here's the code below:
cmd = 'curl -X GET -G "http://url.com/lat/long" -d start="2018-04-01" -d end="2018-04-04" --user abcd:1234'
p = sp.Popen(cmd, shell=True,stdout=sp.PIPE, stderr=sp.STDOUT).stdout.read()
p.split('\n')[3]
Here's the code I have for requests.get:
r=requests.get("https://url.com/lat/long",auth=HTTPBasicAuth('user','password'))
However, cannot pass the date arguments through to limit my data pull to my specified date ranges.
Here's a couple of method's I have tried.
r=requests.get("https://url.com/lat/long",start=start,end=end,auth=HTTPBasicAuth('user','password'))
Also tried:
data = [
('startDate', '2018-04-01'),
('endDate', '2018-04-08'),
]
r=requests.get("https://url.com/lat/long",data=data,auth=HTTPBasicAuth('user','password'))
Can someone guide me in the right direction? I really want to stick with the request library command, but only thing thats working right now is running the curl command through the subprocess method, and I don't fully understand that process.
Let's take a look at the G parameter in your curl command:
-G, --get Put the post data in the URL and use GET
This means that your data will not be submitted in the body of the request, but in the url as a query string. With requests you can use the params argument to achieve that.
So your curl command is equivalent to:
import requests
url = 'https://url.com/lat/long'
data = {'start': '2018-04-01', 'end': '2018-04-08'}
creds = ('user','password')
r = requests.get(url, params=data, auth=creds)
print(r.text)
try
data = {
'start': '2018-04-01',
'end': '2018-04-08'
}