So I'm testing out a stock market API, using alpha vantage. I've already got my API Key, and other parameters and followed the steps that have been given in the API documentation.
Here's my code:
import requests
url = "https://www.alphavantage.co.query?function=TIME_SERIES_INTRADAY&symbol=TSLA&interval=1min&apikey=IJP9K6ENTHAVAUHF"
response = requests.get(url)
data = response.json()
print(data)
now according go the docs, This should work. I've already entered the parameters such as the symbol for TSLA (tesla), a time interval of 1 minute, and my own api key
But for some reason this code just does not work
Here's the error
Traceback (most recent call last):
File "c:\Users\Armand S\Desktop\Python Files\D36 Stock Trading API.py", line 28, in <module>
response = requests.get(url)
File "C:\Users\Armand S\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Armand S\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Armand S\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Armand S\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Armand S\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 501, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
Any help would be much appreciated!
Related
I've written a little Code in python in order to get the current weather from https://openweathermap.org/. However, when I run my Code, I get the error:
"Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\app\Python36\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\app\Python36\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\app\Python36\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\app\Python36\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "C:\app\Python36\lib\site-packages\requests\adapters.py", line 508, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.openweathermap.org', port=80): Max retries exceeded with url: /data/2.5/weather?appid=298b44e1966f7870841309311c2988e9&q=London (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000024DB4CD9710>: Failed to establish a new connection: [WinError 10061]"
If I send the same request via https://apitester.com/, then my URL works, i.e. I get a json-file back with weather informations. So I assume something is wrong with my python Code or Windows cannot handle it:
import requests
def get_weather():
url = "http://api.openweathermap.org/data/2.5/weather?appid=298b44e1966f787084fftrhbbgzzum&q=London"
r = requests.get(url)
return r.json()
def main():
weather = get_weather()
print(weather)
main()
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
I am using Python 2.7.3, Requests version 2.7.0. I have setup a simple HTTPS server using a self signed certificate. I can run the following script on my Windows fine. But I am unable to do the same on an embedded device. On the embedded device I have no problem with http get or https with verify=False.
import requests
url = 'https://192.168.3.4:8002'
requests.get(url, verify='/home/root/server.crt')
The error statement:
Traceback (most recent call last):
File "test.py", line 3, in <module>
requests.get(url, verify='/home/root/server.crt')
File "/usr/lib/python2.7/site-packages/requests/api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno bad handshake] [('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')]
Does anyone have any idea?
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'))