AttributeError: 'function' object has no attribute 'response' - python

I have an error handling module that I'm using in my main script to make requests to an API. I want to return "response" and "data" to use in the main script. It works up until trying to print "response". Apologies for inconsistencies, I am obviously still learning. And I won't learn without making a few mistakes first. I appreciate constructive criticism.
my_module
import requests
import json
def errorHandler(url):
try:
response = requests.get(url, timeout=5)
status = response.status_code
data = response.json()
except requests.exceptions.Timeout:
print "Timeout error.\n"
except requests.exceptions.ConnectionError:
print "Connection error.\n"
except ValueError:
print "ValueError: No JSON object could be decoded.\n"
else:
if response.status_code == 200:
print "Status: 200 OK \n"
elif response.status_code == 400:
print "Status: " + str(status) + " error. Bad request."
print "Correlation ID: " + str(data['correlationId']) + "\n"
else:
print "Status: " + str(status) + " error.\n"
return response
return data
my_script
errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n" #expect geo+json
# I need the data from the module to do this, but not for each get request
nwsId = data['features'][0]['properties']['id']
error
Traceback (most recent call last):
File "my_script.py", line 20, in <module>
print errorHandler.response
AttributeError: 'function' object has no attribute 'response'

If you want to return multiple values, you return them as a tuple in a single statement:
return response, data
Then in the caller, you assign them to variables with a tuple assignment:
response, data = errorHandler("https://api.weather.gov/alerts/active")
print "Content type is " + response.headers['content-type'] +".\n"
nwsId = data['features'][0]['properties']['id']
However, your function will not work correctly if any of the exceptions occur. If there's an exception, it won't set the variables response or data, so when it tries to return them it will get an error.

Related

401. "str' object has no attribute 'read'

The code below produces these errors:
'Response' object has no attribute 'read'
Please help me understand what I did wrong. I made sure un and pwd are correct
user = "un"
password = 'pwd'
datanodes = ["https://server040:25000/"]
for i, datanode in enumerate(datanodes):
print("Checking {0}: {1}".format(i, datanode))
try:
print "trying"
response = requests.get(datanode + "queries?json",auth=(user,
password), verify='certs.pem')
print response
data = json.loads(response.read())
print data
if data["num_waiting_queries"] > 0:
print(data["num_waiting_queries"])
for in_flight_query in data["in_flight_queries"]:
if in_flight_query["waiting"] is True and
in_flight_query['state'] == "FINISHED":
cancel_url = datanode + "cancel_query?query_id=
{}".format(in_flight_query['query_id'])
print(cancel_url)
except IOError as ioe:
print ioe
except Exception as e:
print(e)
I have tried both json.load(reponse) and json.loads(response.read())
I was able to get around the issues by adding HTTPDigestAuth and changing data = json.loads(response.read()) to data = response.json()

Python Beautiful Soup Error : list index out of range

I am getting the following error with the script, i want to do is,
if i get a correct URL, i want it to check with BeautifulSoup if there is a form with value button "Send"
Traceback (most recent call last):
File "tester.py", line 27, in
if viewstate[0]['value'] == "Send":
IndexError: list index out of range
#!/usr/bin/env python
import urllib2
import sys
import os
url = sys.argv[1]
open_dir_list = open("dirlist.txt",'r')
dirs = open_dir_list.read().split("\n")
open_dir_list.close()
for dir in dirs:
uri = url+"/"+dir
try:
from BeautifulSoup import BeautifulStoneSoup
response = urllib2.urlopen(uri)
if response.getcode() == 200:
s = uri
soup = BeautifulStoneSoup(uri)
viewstate = soup.findAll("input", {"type": "submit"})
if viewstate[0]['value'] == "Send":
print("Yay!!!")
else:
print("There's nothing here")
except urllib2.HTTPError, e:
if e.code == 401:
print "[!] Authorization Required %s " % (uri)
elif e.code == 403:
print "[!] Forbidden %s " % (uri)
elif e.code == 404:
print "[-] Not Found %s " % (uri)
elif e.code == 503:
print "[!] Service Unavailable %s " % (uri)
else:
print "[?] Unknwon"
print "\n:. FINISH :.\n"
It is working fine with this script, but it only checks only a given path
import urllib
f = urllib.urlopen("http://xxx.xxx.xxx.xxx/button.jsp")
s = f.read()
f.close()
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)
viewstate = soup.findAll("input", {"type": "submit"})
if viewstate[0]['value'] == "Send":
print(" Yay!!!")
else:
print("No Submit Button")
Apart from what I mentioned in a comment you are not passing the returned html you are passing uri = url+"/"+dir instead of response.read() to BeautifulSoup so you are searching for the tag in uri which I imagine certainly does not contain any tags. You need to pass read as below:
response = urllib2.urlopen(uri)
if response.getcode() == 200:
soup = BeautifulStoneSoup(response.read())
If you want the first match use .find using if viewstate to make sure it matched something, you can also iterate over the file object getting a line at a time:
from BeautifulSoup import BeautifulStoneSoup
import urllib2
import sys
url = sys.argv[1]
with open("dirlist.txt",'r') as f:
for dir in f:
uri = url + "/" + dir.rstrip()
try:
response = urllib2.urlopen(uri)
if response.getcode() == 200:
soup = BeautifulStoneSoup(response.read())
viewstate = soup.find("input", {"type": "submit"})
if viewstate and viewstate.get("value") == "Send":
print("Shell is found!! Yay!!!")
else:
print("There's nothing here")
except urllib2.HTTPError, e:
if e.code == 401:
print "[!] Authorization Required %s " % (uri)
elif e.code == 403:
print "[!] Forbidden %s " % (uri)
elif e.code == 404:
print "[-] Not Found %s " % (uri)
elif e.code == 503:
print "[!] Service Unavailable %s " % (uri)
else:
print "[?] Unknwon"
print "\n:. FINISH :.\n"

Type error in python program

I always get a Type Error when I run the following python code (abc.py) as follows:
./abc.py activatelink alphabeta
Type Error: ['alphabeta']
My code:
#!/usr/bin/python
import urllib2
from urllib2 import URLError
from urllib2 import HTTPError
import requests
import urllib
import json
import time
import os
import sys
import hashlib
def activate_user(link):
print invoke_rest('GET', link)
def invoke_rest(request_type, rest_url, payload, headers):
try:
api_url = rest_url
if request_type == 'GET':
r = requests.get(api_url)
to_ret = {'code':r.status_code, 'reply':r.text}
return to_ret
elif request_type == 'POST':
r = requests.post(api_url, data=payload, headers=headers)
to_ret = {'code':r.status_code, 'reply':r.text}
return to_ret
else:
return "Invalid request type ", request_type
except Exception, e:
return "Exception:", e, " in getting the API call"
def help():
print ('Usage: %s { activate | help }', os.path.basename(sys.argv[0])
if __name__ == '__main__':
actions = {'activatelink': activate_user, 'help': help}
try:
action = str(sys.argv[1])
except IndexError:
print "IndexError: ", sys.argv[1]
action = 'help'
args = sys.argv[2:]
try:
actions[action](*args)
except (KeyError):
print "Key Error:", args
help()
except (TypeError):
print "Type Error:", args
help()
Am I doing anything wrong? I added some other functions other than activatelink, which work fine, can anyone point out whats wrong in here?
Your invoke_rest() function takes four arguments:
def invoke_rest(request_type, rest_url, payload, headers):
but you pass in just the two:
print invoke_rest('GET', link)
That raises a TypeError exception:
>>> def invoke_rest(request_type, rest_url, payload, headers):
... pass
...
>>> invoke_rest('GET', 'alphabeta')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: invoke_rest() takes exactly 4 arguments (2 given)
Perhaps you wanted those two extra arguments (payload and headers) to be optional. If so, make them keyword arguments and set their default value to None:
def invoke_rest(request_type, rest_url, payload=None, headers=None):
which is fine by the requests library.

Converting python2 code to python3 problems

So I have been trying too convert an omegle bot, which was written in python2, to python3. This is the original code: https://gist.github.com/thefinn93/1543082
Now this is my code:
import requests
import sys
import json
import urllib
import random
import time
server = b"odo-bucket.omegle.com"
debug_log = False # Set to FALSE to disable excessive messages
config = {'verbose': open("/dev/null","w")}
headers = {}
headers['Referer'] = b'http://odo-bucket.omegle.com/'
headers['Connection'] = b'keep-alive'
headers['User-Agent'] = b'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2'
headers['Content-type'] = b'application/x-www-form-urlencoded; charset=UTF-8'
headers['Accept'] = b'application/json'
headers['Accept-Encoding'] = b'gzip,deflate,sdch'
headers['Accept-Language'] = b'en-US'
headers['Accept-Charset'] = b'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
if debug_log:
config['verbose'] = debug_log
def debug(msg):
if debug_log:
print("DEBUG: " + str(msg))
debug_log.write(str(msg) + "\n")
def getcookies():
r = requests.get(b"http://" + server + b"/")
debug(r.cookies)
return(r.cookies)
def start():
r = requests.request(b"POST", b"http://" + server + b"/start?rcs=1&spid=", data=b"rcs=1&spid=", headers=headers)
omegle_id = r.content.strip(b"\"")
print("Got ID: " + str(omegle_id))
cookies = getcookies()
event(omegle_id, cookies)
def send(omegle_id, cookies, msg):
r = requests.request(b"POST","http://" + server + "/send", data="msg=" + urllib.quote_plus(msg) + "&id=" + omegle_id, headers=headers, cookies=cookies)
if r.content == "win":
print("You: " + msg)
else:
print("Error sending message, check the log")
debug(r.content)
def event(omegle_id, cookies):
captcha = False
next = False
r = requests.request(b"POST",b"http://" + server + b"/events",data=b"id=" + omegle_id, cookies=cookies, headers=headers)
try:
parsed = json.loads(r.content)
for e in parsed:
if e[0] == "waiting":
print("Waiting for a connection...")
elif e[0] == "count":
print("There are " + str(e[1]) + " people connected to Omegle")
elif e[0] == "connected":
print("Connection established!")
send(omegle_id, cookies, "HI I just want to talk ;_;")
elif e[0] == "typing":
print("Stranger is typing...")
elif e[0] == "stoppedTyping":
print ("Stranger stopped typing")
elif e[0] == "gotMessage":
print("Stranger: " + e[1])
try:
cat=""
time.sleep(random.randint(1,5))
i_r=random.randint(1,8)
if i_r==1:
cat="that's cute :3"
elif i_r==2:
cat="yeah, guess your right.."
elif i_r==3:
cat="yeah, tell me something about yourself!!"
elif i_r==4:
cat="what's up"
elif i_r==5:
cat="me too"
else:
time.sleep(random.randint(3,9))
send(omegle_id, cookies, "I really have to tell you something...")
time.sleep(random.randint(3,9))
cat="I love you."
send(omegle_id, cookies, cat)
except:
debug("Send errors!")
elif e[0] == "strangerDisconnected":
print("Stranger Disconnected")
next = True
elif e[0] == "suggestSpyee":
print ("Omegle thinks you should be a spy. Fuck omegle.")
elif e[0] == "recaptchaRequired":
print("Omegle think's you're a bot (now where would it get a silly idea like that?). Fuckin omegle. Recaptcha code: " + e[1])
captcha = True
except:
print("Derka derka derka")
if next:
print("Reconnecting...")
start()
elif not captcha:
event(omegle_id, cookies)
start()
The error I get is:
Traceback (most recent call last):
File "p3.py", line 124, in <module>
start()
File "p3.py", line 46, in start
r = requests.request(b"POST", b"http://" + server + b"/start?rcs=1&spid=", data=b"rcs=1&spid=", headers=headers)
File "/usr/lib/python3.4/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 553, in send
adapter = self.get_adapter(url=request.url)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 608, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'b'http://odo-bucket.omegle.com/start?rcs=1&spid=''
I didn't really understand what would fix this error, nor what the problem really is, even after looking it up.
UPDATE:
Now after removing all the b's I get the following error:
Traceback (most recent call last):
File "p3.py", line 124, in <module>
start()
File "p3.py", line 47, in start
omegle_id = r.content.strip("\"")
TypeError: Type str doesn't support the buffer API
UPDATE 2:
After putting the b back to r.content, I get the following error message:
Traceback (most recent call last):
File "p3.py", line 124, in <module>
start()
File "p3.py", line 50, in start
event(omegle_id, cookies)
File "p3.py", line 63, in event
r = requests.request("POST","http://" + server + "/events",data="id=" + omegle_id, cookies=cookies, headers=headers)
TypeError: Can't convert 'bytes' object to str implicitly
UPDATE 3:
Everytime I try to start it excepts "Derka derka", what could be causing this (It wasn't like that with python2).
requests takes strings, not bytes values for the URL.
Because your URLs are bytes values, requests is converting them to strings with str(), and the resulting string contains the characters b' at the start. That's no a valid scheme like http:// or https://.
The majority of your bytestrings should really be regular strings instead; only the content.strip() call deals with actual bytes.
The headers will be encoded for you, for example. Don't even set the Content-Type header; requests will take care of that for you if you pass in a dictionary (using string keys and values) to the data keyword argument.
You shouldn't set the Connection header either; leave connection management to requests as well.

"NoneType object is not callable" error when getting web page

i'm vey new to python and i wrote this code
def geturl(url):
url = urllib.quote(url,'/:')
log( __name__ ,"Getting url:[%s]" % url)
try:
req = urllib2.Request(url)
response = urllib2.urlopen(req)
content = response.read()
return_url = response.geturl()
response.close()
except Exception, e:
log( __name__ ,"Failed to get url because %s" % str(e))
content = None
return_url = None
return(content, return_url)
def myFunc():
searchurl="http://www.google.com"
socket.setdefaulttimeout(3)
content, response_url = geturl(searchurl)
if content is None:
log( __name__ , "Content is None!!")
content = content.replace("string1", "string2")
when i run it, i get this error: NoneType object is not callable.
i can't understand why... i learned that NoneType is the type of None, but i check if "content" variable is None and it's not.
thx for your help
You're not giving the erroneous line, however I suspect the following to be wrong:
if content is None:
log( __name__ , "Content is None!!")
content = content.replace("string1", "string2")
When content is indeed None, a log is emitted but the function keeps executing, until the call to replace raises an exception.

Categories

Resources