i have this situation where in i am uploading files to a server using Python. here is my python script. this uses apache2 certified requests library
def Upload(self):
Url = 'http://x.php'
Payload = {
'Location' : self.Location,
'Version' : self.Version,
'Variant' : self.Variant,
}
#employing requests
File = {'file' : ('TestFile', open(self.Location, 'rb'))}
response = requests.post(Url, files=File, data=Payload)
The data is received by an apache2 server running on ubuntu machine. I would want to know how to access the sent file from the PHP code and save it in the desired location. Can you please suggest the way to achieve this.
Is there any way like HTTP, where i can specify the that this is like so, i can access using $_FILE["TestFile"]
Please help
-sreekanth
Related
I want to upload files to Slack using Slack API and Python. I tried to use webhooks but it didn't help me send the file. I am able to send messages only but not files. Is there a way to achieve uploading the file?
Finally, this worked for me.
Steps
Create a Slack app. If you are not the admin you need to request the same.
Add the permissions to read, write.
Get the Authorization token from oauth & permissions
Use the following snippet to upload the file.
url = "https://slack.com/api/files.upload"
headers = {
"Authorization":"Bearer xxxx", ----> this is the token you receive from oauth & permissions. It generally starts with xox
}
payload = { "channels":"channelXYZ"}
file_upload = {
"file":("./hello-world.txt",
open("./hello-world.txt", 'rb'), 'text/plain')
}
response = requests.post(url, headers=headers, files=file_upload, data=payload)
I'm creating an app that has to show data of an active directory (which is hosted in microsoft azure) i have been told, that this request of data is going to be accomplished by using the Microsoft Graph API through HTTP GET requests.
Apart from “checking” the active directory data in azure, my app has to be capable of read the local accounts’ permissions, so to acomplish that this wouldn’t be possible to be read from the active directory, which means that I will have to check in the computers itself. For that problem, I've been told that I have to do it through powershell programming (to retrieve data of the local accounts).
At the end my application has to run on AWS Lambda.
More than code i would like guys if you can give me an approach of what to use to get to the solution.
What I have done so far:
I created a python file with this code to connect to the microsoft graph API:
There I added a token to access to the graph API, but i couldn't succeed.
I obtained the token from here, that is supposed to be a sample token of microsoft. For now instead of running my app on AWS Lambda, im running it on pycharm.
My Code on python to access to Azure Active Directory:
import http.client, urllib.request, urllib.parse, urllib.error, base64
# OAuth2 is required to access this API. For more information visit: https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-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fh7hE4qcD7PqHGsykYj7A0XqHCjbKKgWSkcAg=='
}
params = urllib.parse.urlencode({
# Specify values for the following required parameters
'api-version': '1.6'})
try:
conn = http.client.HTTPSConnection('graph.windows.net')
# Specify values for path parameters (shown as {...}) and request body if needed
conn.request("GET", "/myorganization/groups/f795caea-121d-49c7-8ae6-a95623add8aa?%s" % params, "", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
This is the error I get after executing my code:
C:\Users\Joan\PycharmProjects\Prueba\venv\Scripts\python.exe C:/Users/Joan/PycharmProjects/Prueba/GraphTest.py
b'{"odata.error":{"code":"Authentication_MissingOrMalformed","message":{"lang":"en","value":"Access Token missing or malformed."},"date":"2018-07-02T21:01:26","requestId":"c3429e83-9cee-4f79-bfa5-642aa8da482a","values":null}}'
Process finished with exit code 0
This section:
headers = {
'Authorization': 'Bearer' 'EwAoA8l6BAAU7p9QDpi/D7xJLwsTgCg3TskyTaQAAXu71AU9f4aS4rOK5xoO/SU5HZKSXtCsDe0Pj7uSc5Ug008qTI+a9M1tBeKoTs7tHzhJNSKgk7pm5e8d3oGWXX5shyOG3cKSqgfwuNDnmmPDNDivwmi9kmKqWIC9OQRf8InpYXH7NdUYNwN+jljffvNTewdZz42VPrvqoMH7hSxiG7A1h8leOv4F3Ek/oeJX6U8nnL9nJ5pHLVuPWD0aNnTPTJD8Y4oQTp5zLhDIIfaJCaGcQperULVF7K6yX8MhHxIBwek418rKIp11om0SWBXOYSGOM0rNNN59qNiKwLNK+MPUf7ObcRBN5I5vg8jB7IMoz66jrNmT2uiWCyI8MmYDZgAACPoaZ9REyqke+AE1/x1ZX0w7OamUexKF8YGZiw+cDpT/BP1GsONnwI4a8M7HsBtDgZPRd6/Hfqlq3HE2xLuhYX8bAc1MUr0gP9KuH6HDQNlIV4KaRZWxyRo1wmKHOF5G5wTHrtxg8tnXylMc1PKOtaXIU4JJZ1l4x/7FwhPmg9M86PBPWr5zwUj2CVXC7wWlL/6M89Mlh8yXESMO3AIuAmEMKjqauPrgi9hAdI2oqnLZWCRL9gcHBida1y0DTXQhcwMv1ORrk65VFHtVgYAegrxu3NDoJiDyVaPZxDwTYRGjPII3va8GALAMVy5xou2ikzRvJjW7Gm3XoaqJCTCExN4m5i/Dqc81Gr4uT7OaeypYTUjnwCh7aMhsOTDJehefzjXhlkn//2eik+NivKx/BTJBEdT6MR97Wh/ns/VcK7QTmbjwbU2cwLngT7Ylq+uzhx54R9JMaSLhnw+/nIrcVkG77Hi3neShKeZmnl5DC9PuwIbtNvVge3Q+V0ws2zsL3z7ndz4tTMYFdvR/XbrnbEErTDLWrV6Lc3JHQMs0bYUyTBg5dThwCiuZ1evaT6BlMMLuSCVxdBGzXTBcvGwihFzZbyNoX+52DS5x+RbIEvd6KWOpQ6Ni+1GAawHDdNUiQTQFXRxLSHfc9fh7hE4qcD7PqHGsykYj7A0XqHCjbKKgWSkcAg=='
}
Requests an additional space between 'Bearer' and the token itself:
headers = {
'Authorization': 'Bearer ' 'EwAoA8l6BAAU7p9QDpi/D7xJLwsTgCg3TskyTaQAAXu71AU9f4aS4rOK5xoO/SU5HZKSXtCsDe0Pj7uSc5Ug008qTI+a9M1tBeKoTs7tHzhJNSKgk7pm5e8d3oGWXX5shyOG3cKSqgfwuNDnmmPDNDivwmi9kmKqWIC9OQRf8InpYXH7NdUYNwN+jljffvNTewdZz42VPrvqoMH7hSxiG7A1h8leOv4F3Ek/oeJX6U8nnL9nJ5pHLVuPWD0aNnTPTJD8Y4oQTp5zLhDIIfaJCaGcQperULVF7K6yX8MhHxIBwek418rKIp11om0SWBXOYSGOM0rNNN59qNiKwLNK+MPUf7ObcRBN5I5vg8jB7IMoz66jrNmT2uiWCyI8MmYDZgAACPoaZ9REyqke+AE1/x1ZX0w7OamUexKF8YGZiw+cDpT/BP1GsONnwI4a8M7HsBtDgZPRd6/Hfqlq3HE2xLuhYX8bAc1MUr0gP9KuH6HDQNlIV4KaRZWxyRo1wmKHOF5G5wTHrtxg8tnXylMc1PKOtaXIU4JJZ1l4x/7FwhPmg9M86PBPWr5zwUj2CVXC7wWlL/6M89Mlh8yXESMO3AIuAmEMKjqauPrgi9hAdI2oqnLZWCRL9gcHBida1y0DTXQhcwMv1ORrk65VFHtVgYAegrxu3NDoJiDyVaPZxDwTYRGjPII3va8GALAMVy5xou2ikzRvJjW7Gm3XoaqJCTCExN4m5i/Dqc81Gr4uT7OaeypYTUjnwCh7aMhsOTDJehefzjXhlkn//2eik+NivKx/BTJBEdT6MR97Wh/ns/VcK7QTmbjwbU2cwLngT7Ylq+uzhx54R9JMaSLhnw+/nIrcVkG77Hi3neShKeZmnl5DC9PuwIbtNvVge3Q+V0ws2zsL3z7ndz4tTMYFdvR/XbrnbEErTDLWrV6Lc3JHQMs0bYUyTBg5dThwCiuZ1evaT6BlMMLuSCVxdBGzXTBcvGwihFzZbyNoX+52DS5x+RbIEvd6KWOpQ6Ni+1GAawHDdNUiQTQFXRxLSHfc9fh7hE4qcD7PqHGsykYj7A0XqHCjbKKgWSkcAg=='
}
I'm assuming this is just some scratch POC code but, just in case, keep in mind that your token will expire after 1hr so hardcoding it like this doesn't make sense. You'll want to fetch a new token as needed and inject it into the header when making the call.
I am new at stackoverflow and to programming with python and I am trying to get an emotion analyses for images from my hard disk by using the service of skybiometry.com. The example link of them is like: "http://api.skybiometry.com/fc/faces/detect.json?api_key=aa754b54b37&api_secret=4b3a4c6d4c&urls=http://theweeklyworld.com/wp-content/uploads/2014/08/child-happy-face1.jpg&attributes=all" and I want to do this in my python-script with my image. On their website https://skybiometry.com/documentation/ on point 4.13 they said that the request has to be formed as a MIME if I want to analyze images from my hard disk. I do not know how to handle this. In an other project of mine I have done the request like this
import requests
auth_headers = {
'api_key': api_key,
'api_secret': api_secret,
}
url = 'http://api.skybiometry.com/fc/faces/detect'
files = { 'source': open(path + ".jpg", 'rb')
}
data = { 'timeout': 60
}
response = requests.post(url, files=files, data=data, headers=auth_headers)
print (response.json())
Can anyone help me to adjust this request to make it work?
Thanks a lot!
You need to change api_key and api_secret for your own skybiometry credentials to use that python script.
Anyway, I prefer installing the api client skybiometry first for python and then use your python scripts. To install it you need to follow these steps:
git clone git#github.com:SkyBiometry/python-face-client.git
cd python-face-client
python setup.py build
python setup.py install
Then you can use the api-client using import with your skybiometry credentials, for example:
from face_client import FaceClient
client = FaceClient('API_KEY', 'API_SECRET')
Changing the API_KEY and API_SECRET for your own skybiometry credentials.
For more examples and how to use the api-client, you can watch this: https://github.com/SkyBiometry/python-face-client
Greetings.
i'm trying to comunicate with a webserver with python. I'm using the suds library. Actually i'm pretty new with this.
Usually, to comunicate with this WebServer a send a xml message and i get a response. So this is what i would like to do with python.
Here's the code i wrote:
from suds.client import Client
with open("PATH","r") as f:
file=f.read()
url='URL'
client = Client(url)
httpHeaders = {'Content-Type': 'text/xml', 'SOAPAction': 'ACTION'}
client.set_options(headers=httpHeaders)
Now i don't know how to make the request. I tried this:
print client.service.test(__inject={'msg': file})
But i got the error:
Exception: No services defined
The problem seems clear, but i don't know haw to procede. Any suggestion ?
I'm implementing a push notifications mechanism for my android app.
Right now I'm trying to run a small test just to see that I manage to send push notifications from a python server via http to GCM, and to recieve it successfuly in an android client.
Client code is exactly as in google's tutorial:
http://developer.android.com/google/gcm/client.html
There's a main activity called DemoActivity which is responsible for registering or retrieving an already existed a registration id, and two classes GcmIntentService and GcmBroadcastReceiver responsible for handling the messags from the GCM server to the app.
Of course, I've set the SENDER_ID correctly, and I do manage to get a registration ID for my client app.
Now for the server:
In the server I always recieve the following error:
HTTP Error 401: Unauthorized
This is my server code:
url = "https://android.googleapis.com/gcm/send"
headers = { 'Content-Type' : 'application/json', 'Authorization': 'key=' + SERVER_API_KEY }
values = { 'registration_ids': [CLIENT_REGID]
, 'data': {'test': 'test} }
data = urllib.urlencode(values)
req = urllib2.Request(url, json.loads(values), headers)
response = urllib2.urlopen(req)
the_page = response.read()
self.response.out.write(the_page)
For security reasons I omitted the server api key and the client registration id (they're hard-coded), but I double and triple checked them and they're correct. Also, I made sure the server API key was formed correctly (Credentials -> Create new key -> Server key) and also made sure "Any IP allowed".
All solutions I found on the internet was related to a mistake in the server api or something like that but I already checked that.
Thanks for helpers!
edit:
added 'key=' in the header, but now I recieve Bad request error (HTTP code 400)
another edit:
changes the values object abit and sent it using json.loads, but not I have this error in the client (means it finally recieves a notification from server!!):
Unable to instantiate receiver GcmBroadcastReceiver
Any ideas? I copied the sample project from google as is, so I don't have any idea what's wrong here.
The auth header should be (note the key= part):
Authorization:key=<your_key_here>
so you should set headers like this:
headers = { 'Content-Type' : 'application/json', 'Authorization': 'key='+SERVER_API_KEY }
I believe the issue is that you're sending urlencoded payload, when telling the server to expect json. Try changing data to a json object, as a string: data ="{ 'registration_ids':...}"