[英]Issues with Curl request in python
I am attempting to get a simple JSON currency rate response from an API (oanda). 我正在尝试从API(oanda)获取简单的JSON货币汇率响应。
but receiving various error codes, such as 'invalid syntax'. 但收到各种错误代码,例如“无效语法”。
Here is my updated code: 这是我更新的代码:
import requests
import json
from optparse import OptionParser
def connect_to_stream():
"""
Environment <Domain>
fxTrade stream-fxtrade.oanda.com
fxTrade Practice stream-fxpractice.oanda.com
sandbox stream-sandbox.oanda.com
"""
# Replace the following variables with your personal ones
domain = 'stream-fxpractice.oanda.com'
access_token = 'xxxxxxxxxxxxxxxx'
account_id = 'xxxxxxxxx'
instruments = "EUR_USD"
try:
s = requests.Session()
url = "https://" + domain + "/v1/prices"
headers = {'Authorization' : 'Bearer ' + access_token,
# 'X-Accept-Datetime-Format' : 'unix'
}
params = {'instruments' : instruments, 'accountId' : account_id}
req = requests.Request('GET', url, headers = headers, params = params)
pre = req.prepare()
resp = s.send(pre, stream = True, verify = False)
return resp
except Exception as e:
s.close()
print "Caught exception when connecting to stream\n" + str(e)
response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD")
data = json.load(response)
print data
Sorry, I edited the code and left out the error messages.. I was, however, able to solve the problem by using Oanda's python wrapper here https://github.com/oanda/oandapy . 抱歉,我编辑了代码,并保留了错误消息。.但是,我可以通过使用https://github.com/oanda/oandapy上的 Oanda的python包装器解决此问题。
The url is invalid, I'd suggest checking whether the API has recently changed, as a search on it reveals that other people have tried using it in the past. 网址无效,建议您检查一下API是否最近已更改,因为对其进行搜索后发现其他人过去曾尝试使用它。 When trying to access the url in chrome I receive the following information: 尝试访问Chrome中的网址时,我收到以下信息:
The server at api-practice.oanda.com can't be found, because the DNS lookup failed. 找不到api-practice.oanda.com上的服务器,因为DNS查找失败。 DNS is the network service that translates a website's name to its Internet address. DNS是将网站名称转换为其Internet地址的网络服务。 This error is most often caused by having no connection to the Internet or a misconfigured network. 此错误通常是由于没有连接到Internet或配置错误的网络引起的。 It can also be caused by an unresponsive DNS server or a firewall preventing Google Chrome from accessing the network. 原因还可能是DNS服务器没有响应或防火墙阻止了Google Chrome浏览器访问网络。
Error code: DNS_PROBE_FINISHED_NXDOMAIN 错误代码:DNS_PROBE_FINISHED_NXDOMAIN
Also, I would stick to using the requests library or urllib2. 另外,我会坚持使用请求库或urllib2。 The request at the bottom of your question can be rewritten from: 您可以将问题底部的请求改写为:
response = urllib2.urlopen("https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD")
data = json.load(response)
print data
to: 至:
r = requests.get("https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD")
data = r.json()
print data
EDIT: Url updated to match edit in question. 编辑:网址已更新,以匹配相关的编辑。
https://api-practice.oanda.com/v1/prices?instruments=EUR_USD https://api-practice.oanda.com/v1/prices?instruments=EUR_USD
to 至
" https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD " “ https://api-fxpractice.oanda.com/v1/prices?instruments=EUR_USD ”
请从服务器的命令提示符处执行ipconfig / flushdns,您很可能会得到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.