SSRS Download from Python Giving Corrupt File - python

When I run my script, it creates a corrupt, not-openable PDF file.
Here is my code, which seems to work for a lot of other people:
filename = f'./report.pdf'
username = "myusername"
password = "mypassword"
url = "http://RptServerIp/ReportServerName/reports/Untitled&rs:Command=Render&rs:Format=PDF"
r = requests.get(url, auth=HttpNtlmAuth(username, password))
print(r.status_code)
if r.status_code == 200:
with open(filename, 'wb') as out:
for bits in r.iter_content():
out.write(bits)
This is a no-parameter test report (and it's called Untitled). The status code is 200 and I've confirmed the login info is correct by changing one character in the password to make it incorrect, which returns a bad status code. If I go to the URL http://RptServerIp/ReportServerName/reports/Untitled in my browser it shows the report, but if I do the full URL http://RptServerIp/ReportServerName/reports/Untitled&rs:Command=Render&rs:Format=PDF it gives me an error.
Any suggestions?

Thank you to everyone that answered, I found a solution that for some reason isn't everywhere on stack overflow.
The URL I was using was something like:
http://ServerIpAddress/Reports/report/Untitled&rs:Command=Render&rs:Format=PDF
but it needed to be changed to:
http://ServerIpAddress/Reportserver?/Untitled&rs:Command=Render&rs:Format=PDF
The big difference is that the default client name when going through the browser is Reports/{admin-named-subfolder} but the default when using the URL connect is Reportserver? with no subfolder included.

Related

requests.Session() not working even though the post is working

I'm trying to get a bunch of PDFs off of a site that sits behind a login so I don't manually have to download each and every one. I figured this would be easy, but I'm getting a "Missing Key-Pair-Id query parameter" error back. Here's what I have:
payload={'username':'user','password':'pass'}
with requests.Session() as session:
post = session.post('https://website.com/login.do', data=payload)
r = session.get('https://files.website.com/1.pdf')
print(r.text)
I'm printing r.text just because that's where I'm getting the above message. My post variable is giving me a response of 200 and the contents post.text is a redirect link with "code: success", too. If I click that link (or copy paste it into a private browser), I'm logged in just fine. And browsing to the pdf link works just fine. What am I missing here? Thanks.

Python auth returns 401

I have a python script that downloads a snapshot from a camera. I use auth to login to the camera. For older style cameras it works with no issues, with the new one it doesn't. I have tested the link and credentials by copying them from my python script to make sure they work and they do, but i still can't login and I am not sure why. The commented url is the one that works. The uniview one doesn't. I have replaced the password with the correct one and i also tested the link in Chromium and it works.
import requests
#hikvision old cameras
#url = 'http://192.168.100.110/ISAPI/Streaming/channels/101/picture'
#uniview
url = 'http://192.168.100.108:85/images/snapshot.jpg'
r = requests.get(url, auth=('admin','password'))
if r.status_code == 200:
with open('/home/pi/Desktop/image.jpg', 'wb') as out:
for bits in r.iter_content():
out.write(bits)
else:
print(r.status_code)
print(r.content)
Below is the response I get
b'{\r\n"Response": {\r\n\t"ResponseURL": "/images/snapshot.jpg",\r\n\t"ResponseCode": 3,\r\n \t"SubResponseCode": 0,\r\n \t"ResponseString": "Not Authorized",\r\n\t"StatusCode": 401,\r\n\t"StatusString": "Unauthorized",\r\n\t"Data": "null"\r\n}\r\n}\r\n'
So it looks like hikvisio are using Basic_access_authentication while uniview are using Digest_access_authentication so according to the docs you need to change your request to:
from requests.auth import HTTPDigestAuth
r = requests.get(url, auth=HTTPDigestAuth('admin','password'))

Python Requests: Check if Login was successful

I've looked all over the place for the solution I'm looking for but just can't find it. Basically, I'm developing a tool which takes a list of URLs from a text document, logs into them with your username/password, and returns which ones work.
I have the login figured out and all, but how do I actually return if the login works? Sites entered on the list won't necessarily use cookies and will be developed on various platforms.
Here's my login code:
r = requests.post(action, data=values, verify=False)
print(r.headers)
print(r.status_code)
print(r.content)
print(r.url)
sleep(1)
I'd like everything after that first line to be in an if statement if the login actually works, but I can't figure out how to actually determine that.
Thanks.
You can parse the r.content to check if you login or not, like string success or some flag means that you login.
if the r.content for login user and not login user are same, you can request a special link that need to login and find a flag to check.
status bit will let you know
r = requests.post(action, data=values, verify=False)
authi = requests.get(r.url, auth=('user', 'pass'))
>>> authi.status_code
200
You will find more status bit info here
STATUS CODE DEFINITION

Python - check if file/webpage exists

I would like to use Python to check if a file/webpage exists based off its response code and act accordingly. However, I have a requirement to use HTTPS and to also provide username and password credentials. I couldn't get it running through curl (doesn't like HTTPS) but had success by using wget (with --spider and --user and --password). I suppose I can try incorporating wget into the script via os.system but it prints out a lot of output that would be very tricky to parse and if the URI does not exist (aka 404), I think gets stuck "awaiting response..".
I've had a look at urllib2 around the web and have seen people do some stuff, but I'm not sure if this addresses my situation and the solutions are always very convoluted (such as Python urllib2, basic HTTP authentication, and tr.im) . Anyway, if I can get some guidance on what the easiest avenue for me to pursue is using python, that would be appreciated.
edit: using the os.system method (and providing wget with "-q") seems to return a different number if the URI exists or not, so that gives me something to work with for now.
You can make a HEAD request using python requests.
import requests
r = requests.head('http://google.com/sjklfsjd', allow_redirects=True, auth=('user', 'pass'))
assert r.status_code != 404
If the request fails with a ConnectionError, the website does not exist. If you only want to check whether a certain page exists, you will get a successful response but the status code will be 404.
Requests has a pretty nice interface so I recommend checking it out. You'll probably like it a lot as it is very intuitive and powerful (while being lightweight).
urllib2 is the way to go to open any web page
urllib2.urlopen('http://google.com')
for added functionality, you'll need an opener with handlers. I reckon you'll only need the https because you're barely extracting any info
opener = urllib2.build_opener(
urllib2.HTTPSHandler())
opener.open('https://google.com')
add data and it will automatically become a POST request, or so i believe:
opener.open('https://google.com',data="username=bla&password=da")
the object you'll receive will have a code attribute.
That's the basic gist of it, do add as many handlers as you like, i believe they can't hurt.
source: https://docs.python.org/2.7/library/urllib2.html#httpbasicauthhandler-objects
You should use urllib2 to check that:
import urllib2, getpass
url = raw_input('Enter the url to search: ')
username = raw_input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
if not url.startswith('http://') or not url.startswith('https://'):
url = 'http://'+url
def check(url):
try:
urllib2.urlopen(url)
return True
except urllib2.HTTPError:
return False
if check(url):
print 'The webpage exists!'
else:
print 'The webpage does not exist!'
opener = urllib2.build_opener(
urllib2.HTTPSHandler())
opener.open(url,data="username=%s&password=%s" %(username, password))
This runs as:
bash-3.2$ python url.py
Enter the url to search: gmail.com
Enter your username: aj8uppal
Enter your password:
The webpage exists!

Python Requests GET fails after successfull session login

So I'm using the Python Requests library to login to a PHP-WebCMS. So far I was able to login using the post-command with a payload. I am able to download a file.
The problem is: When I'm running the GET-Command just after loggin in via POST it tells me that im not logged in anymore - although I'm still using the same session! Please have a look at the code
#Lets Login
with requests.session() as s:
payload = {'username': strUserName, 'password': strUserPass, 'Submit':'Login'}
r = s.post(urlToLoginPHP, data=payload, stream=True)
#Ok we are logged in. If I would run the #DOWNLOADING Files code right here I would get a correct zip file
#But since the r2-Get-Command delivers the "please login" page it doesn't work anymore
r2 = s.get("urlToAnotherPageOfThisWebsite",stream=True)
#We are not logged in anymore
#DOWNLOADING Files: This now just delivers a 5KB big file which contains the content
of the "please login page"
local_filename = 'image.zip'
# NOTE the stream=True parameter
r1 = s.get(downloadurl, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r1.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
I found the solution: I was logged into the Browser and was trying to login via python at the same time. The site noticed that I was already logged in and I misunderstood it.

Categories

Resources