I am trying to post the below request and hitting the traceback. Same scripts works with a different server, but doesn't work from my Mac.
Code
url = 'https://172.31.192.28/api/nxosmock/mo/sys/phys-[eth1/30].xml'
xml_data='''
<l1PhysIf adminSt="up" autoNeg="on" bw="0" childAction="" delay="1" descr="" dn="sys/phys-[eth1/30]" dot1qEtherType="0x8100" ethpmCfgFailedBmp="" ethpmCfgFailedTs="00:00:00:00.000" ethpmCfgState="0" id="eth1/30" inhBw="unspecified" layer="Layer2" lcOwn="local" linkDebounce="100" linkLog="default" mdix="auto" medium="broadcast" modTs="2015-11-02T22:53:14.007+00:00" mode="trunk" monPolDn="" mtu="9216" name="" pathSDescr="" portT="unknown" routerMac="not-applicable" snmpTrapSt="enable" spanMode="not-a-span-dest" speed="10G" status="" switchingSt="enabled" trunkLog="default" usage="discovery"/>
#'''
# POST
print 'trying to post interface bringup for SVI'
resp = requests.post(url, data=xml_data, headers=headers, verify=False)
print resp.status_code
Hitting below error
Traceback (most recent call last):
File "raj-svi-ipv6-interface-bringup-post.py", line 12, in <module>
resp = requests.post(url, data=xml_data, headers=headers, verify=False)
File "/Library/Python/2.7/site-packages/requests-2.8.1-py2.7.egg/requests/api.py", line 109, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.8.1-py2.7.egg/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.8.1-py2.7.egg/requests/sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests-2.8.1-py2.7.egg/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests-2.8.1-py2.7.egg/requests/adapters.py", line 412, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))
Related
headers = {'Accept': 'application/json',}
response = requests.get(self.url, headers=headers, verify=self.ssl)
Getting error when passing False as input from command line. It works fine when verify=False is set in the code without passing it as input.
Error:
response = requests.get(self.api, headers=headers, verify=self.ssl)
File "/usr/lib/python3/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 520, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 630, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 407, in send
self.cert_verify(conn, request.url, verify, cert)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 226, in cert_verify
"invalid path: {0}".format(cert_loc))
OSError: Could not find a suitable TLS CA certificate bundle, invalid path: False
parser.add_argument('--ssl', default=True, action="store_false")
I am making call for my REST API Get call and it needs proxy authentication(2FA)
So here is my sample code:
import requests
from pypac import PACSession, get_pac
from pypac import PACSession
from requests.auth import HTTPProxyAuth
pac = get_pac(url='https://abcd.com')
session = PACSession(pac)
session.proxy_auth = HTTPProxyAuth('rkrisha', 'Yubikeycode')
url = 'http://company.com/sampleapi/v1/logs/errors?startDt=2018-03-26&endDt=2018-03-26'
response = requests.get(url,headers={"Authorization":"Token"})
print(response.text)
Getting following Error stack:
File "/Users/ynaik/PycharmProjects/Udemy/Python_practice/REST_Get.py", line 12, in <module>
response = requests.get(url,headers={"Authorization":"Token"})
File "/Users/ynaik/PycharmProjects/Udemy/venv/lib/python3.6/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/Users/ynaik/PycharmProjects/Udemy/venv/lib/python3.6/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/ynaik/PycharmProjects/Udemy/venv/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/Users/ynaik/PycharmProjects/Udemy/venv/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/Users/ynaik/PycharmProjects/Udemy/venv/lib/python3.6/site-packages/requests/adapters.py", line 490, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
Can someone please take a look and let me know?
How to make 2FA authentication using ubikey and then make a Get API call?
Thanks.
this question is duplicate. but i am still asking because i am not finding any solution from given answers. so guys don't make it duplicate, instead help me to solve this problem.
i am running my server as https using bottle(paste). when trying to access from java and postman , it's verifying correctly and response coming.
versions used :
python 2.7.13
requests.version
'2.11.1'
import cryptography
cryptography.version
'2.1.3'
import OpenSSL
OpenSSL.version
'17.3.0'
but unfortunately when trying to access from python modules - it's throwing issue. ?
requests.post(url, verify=_certfile, proxies=proxies) #tried #failed to verify certificate
urllib2.urlopen(url, data=data, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
cafile=None, capath=None, cadefault=False, context=context) #tried #failed to verify certificate
import httplib2
http = httplib2.Http(disable_ssl_certificate_validation=True)
headers, content = http.request(url, "POST", body=data, headers=headers)#tried #failed to verify certificate
httplib.HTTPSConnection(host='10.201.41.50', port=8018, key_file=key_file, cert_file=cert_file, strict=None, context=context) #tried #failed to verify certificate
and my server is running using paste server using bottle framework.
ssl_cxt = SSL.Context(SSL.SSLv23_METHOD)
...
...
ssl_cxt.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb)
paste.httpserver.serve(app, host=IP, port=PORT, server_version=" ", ssl_context=ssl_cxt)
issues i am facing (different with each lib)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Continuum\Anaconda2\lib\site-packages\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: unknown error (_ssl.c:2947)
============
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Continuum\Anaconda2\lib\site-packages\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "Continuum\Anaconda2\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate
verify failed (_ssl.c:661)
File "Continuum\Anaconda2\lib\httplib.py", line 1038, in endheaders
self._send_output(message_body)
File "Continuum\Anaconda2\lib\httplib.py", line 882, in _send_output
self.send(msg)
File "Continuum\Anaconda2\lib\httplib.py", line 844, in send
self.connect()
File "Continuum\Anaconda2\lib\httplib.py", line 1262, in connect
self.sock = self._context.wrap_socket(self.sock,
AttributeError: 'Context' object has no attribute 'wrap_socket'
I tried to solve using urllib2, socket and ssl. i am able to hit the server. i tried with .pkcs12 but seems urllib2 expects only .pem files. so will advice try to create client certificate with .pem extension specifically when you try to access from python.
import ssl
import socket
import urllib2
import json
url = "https://localhost:port/hello/"
_certfile_pem = "ssl_client.pem"
key_file = "ssl_key.pem"
cert_file = "ssl_cert.pem"
headers = {"Content-Type":"application/json"}
data = {}
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED
context.load_cert_chain(certfile=cert_file, keyfile=key_file)
context.load_verify_locations(_certfile_pem)
context.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) #
socket overwrite required.
try:
req = urllib2.Request(url,data=json.dumps(data))
response = urllib2.urlopen(req, context=context)
content = response.read()
print content
except urllib2.HTTPError as e:
content = e.read()
print "HTTPError : ", e
By default, Responses will raise an assertion error if a url was registered but not accessed. How can one do the opposite i.e. raise an error if a url was accessed but not registered?
If you request a URL that was not registered, you will get a
requests.exceptions.ConnectionError
Example
import responses
import requests
#responses.activate
def test_my_api():
resp = requests.get('https://twitter.com/')
print resp
def run_my_api():
resp = requests.get('https://twitter.com/')
print resp
if __name__ == '__main__':
print "Without responses..."
run_my_api()
print ""
print "With responses..."
test_my_api()
Output
Without responses...
<Response [200]>
With responses...
Traceback (most recent call last):
File "example.py", line 18, in <module>
test_my_api()
File "<string>", line 3, in wrapper
File "example.py", line 6, in test_my_api
resp = requests.get('https://twitter.com/')
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/responses.py", line 294, in unbound_on_send
return self._on_request(adapter, request, *a, **kwargs)
File "/usr/local/lib/python2.7/site-packages/responses.py", line 239, in _on_request
raise response
requests.exceptions.ConnectionError: Connection refused: GET https://twitter.com/
The <Response [200]> is from the undecorated run_my_api call. You can use assertRaises or the equivalent to check for the exception.
When i try to run a sample script which sends SOAP request to a HTTPS URL getting below error `
File "H:/PyCharm/Sample.py", line 94, in <module>
response = requests.post(url, data=body, headers=headers)
File "C:\Python27\lib\site-packages\requests\api.py", line 110, in post
return request('post', url, data=data, json=json, **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 485, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host= <myhost>,port=443)Max retries exceeded with url: <myurl>`
I was not able to use requests or urllib3 or SOAPpy to send SOAP to HTTPS. PycURL came to the rescue. I was able to do with a 5 line pyCURL implementation.