I'm trying to get access to the BambooHR API (documentation here), but I receive the following error
params = {
'user': username,
'password': password,
'api_token': api_key}
url = 'https://api.bamboohr.com/api/gateway.php/company/v1/login'
r = requests.get(url, params=params)
Error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 31, in <module>
BambooFunctions().login()
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 26, in login
r = requests.get(url, params=params, auth=HTTPBasicAuth(api_key, 'api_token'))
File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL23_GET_SERVER_HELLO', 'unknown protocol')],)",)
I'm unsure what this is caused by as I've re-installed OpenSSL, Requests, and not sure how to fix this issue.
You try by setting verify=False, use this option if you are using self-signed certificates.
r = requests.get(url, params=params, verify=False)
More info http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification
Related
I was working with a local server when I encountered an error. It is a simple JSON string that I would like to send to the server. Although the string was successfully retrieved by the server, it ended up with an error in the code where the request was sent.
Error:
Traceback (most recent call last):
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
httplib_response = conn.getresponse()
File "...\Programs\Python\Python39\lib\http\client.py", line 1371, in getresponse
response.begin()
File ...\Programs\Python\Python39\lib\http\client.py", line 319, in begin
version, status, reason = self._read_status()
File "...\Programs\Python\Python39\lib\http\client.py", line 301, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: Test
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 440, in send
resp = conn.urlopen(
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "...\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)
File "...\Programs\Python\Python39\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
raise value.with_traceback(tb)
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "...\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
httplib_response = conn.getresponse()
File "...\Programs\Python\Python39\lib\http\client.py", line 1371, in getresponse
response.begin()
File "...\Programs\Python\Python39\lib\http\client.py", line 319, in begin
version, status, reason = self._read_status()
File "...\Programs\Python\Python39\lib\http\client.py", line 301, in _read_status
raise BadStatusLine(line)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('Test'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...\Runner.py", line 14, in <module>
r = requests.post(
File "...\Programs\Python\Python39\lib\site-packages\requests\api.py", line 117, in post
return request('post', url, data=data, json=json, **kwargs)
File "...\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "...\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "...\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "...\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 501, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('Test'))
Code requests:
import requests
header = {
'Content-Type': "application/json"
}
content ={
"ID": "tyhsb12"
}
r = requests.post(
"http://192.168.43.211:8080/",
headers=header,
json= content
)
print(r.text)
Server Code:
import webbrowser
from http.server import HTTPServer, BaseHTTPRequestHandler
class helloHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(str("Test"), "utf-8"))
def do_POST(self):
self.send_response(200)
self.send_header('content-type', "application/json")
self.end_headers
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len,)
post_body2 = post_body.decode('utf-8')
print(post_body2)
self.wfile.write(bytes(str("Test"), "utf-8"))
def main():
PORT = 8080
server = HTTPServer(('192.168.43.211', PORT), helloHandler)
print("Server Started " + str(PORT))
webbrowser.open("http://192.168.43.211:" + str(PORT) + "/" )
server.serve_forever()
if __name__ == '__main__':
main()
just edit one line:
import requests
header = {
'Content-Type': "application/json"
}
content ={
"ID": "tyhsb12"
}
r = requests.post(
"http://192.168.43.211:8080/",
headers=header,
data= content # <===; edit this line (use "data=" instead of "json=")
)
print(r.text)
In Python Requests module, using proxy connection to browse some url
I added proxy ip and port to https_proxy and http_proxy environment variables to route the traffic to proxy
import os
import requests
os.environ["HTTPS_PROXY"] = "proxy1.xx.local:8081"
os.environ["HTTP_PROXY"] = "proxy1xx.local:8081"
H = {"X-Authenticated-User": "bharani#dummy.com"}
url = r"https://google.co.in"
r = requests.get(url,headers=H,verify=False)
print r.status_code
print r.text
Response I'm getting from above code
Traceback (most recent call last):
File "pilot.py", line 356, in <module>
r = requests.get(url,headers=H,verify=False)
File "C:\Python27\lib\site-packages\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)
pyopenssl, ndg-httpsclient, pyasn1 are also installed.
Not sure What I'm missing.
When I run my Python script to get data from RESTAPI of an application, I get the following error. I installed PIP and I installed requests package for the python. Here is my below query:
./simpleRunQuery.py <args> <args>
Traceback (most recent call last):
File "./simpleRunQuery.py", line 25, in <module>
res = requests.post(url, auth=(args.username, args.password), data=jsonRequest, headers=headers)
File "/Library/Python/2.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))
I can attach the script, but my script doens't have the lines 112 or 58 or any of these, it's a simple RESTAPI Script that queries and posts results here. Any pointers?
I wrote a python script to fetch definitions and URLs of list of items (I am using a long list with no less than 3000 items).
The script was working fine, I used it several times, but suddenly I started to get the following error:
('Connection aborted.', error(54, 'Connection reset by peer'))
here is the full traceback
Traceback (most recent call last):
File "Wiki.py", line 41, in <module>
page = wikipedia.page(item)
File "/Library/Python/2.7/site-packages/wikipedia/wikipedia.py", line 270, in page
results, suggestion = search(title, results=1, suggestion=True)
File "/Library/Python/2.7/site-packages/wikipedia/util.py", line 28, in __call__
ret = self._cache[key] = self.fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/wikipedia/wikipedia.py", line 103, in search
raw_results = _wiki_request(search_params)
File "/Library/Python/2.7/site-packages/wikipedia/wikipedia.py", line 737, in _wiki_request
r = requests.get(API_URL, params=params, headers=headers)
File "/Library/Python/2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 502, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 612, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))
It seems that the while installing the regular requests python library, it does not install the necessary packages to cope for https connections. Install these by:
pip install requests[security]
I want to sign in to twitter with the code below and scrape twitter's data then:
import requests
import urllib2
with requests.Session() as c:
url = "https://twitter.com/login"
USER = "hadishamgholi74#gmail.com"
PASS = "52518685251868"
c.get(url)
login_data = {"session[username_or_email]": USER, "session[password]": PASS, "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731", "scribe_log": "", "redirect_after_login": "", "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731"}
c.post(url,data = login_data,headers={"Referer":"https://twitter.com"})
page = c.get("https://twitter.com")
print page.content
But it rise this error:
Traceback (most recent call last):
File "C:/Users/Mehdi/PycharmProjects/scrap/login1.py", line 9, in <module>
c.get(url)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 473, in get
return self.request('GET', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:581)
What should i do?
According to this post, there is not support for the https protocol yet requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol