通过python写一个网站的API接口遇到的问题。

我python写了一个对接网站api接口交易的程序,大部分都搞定了,就是切换endpoint没成功。请问怎么用函数来切换endpoint对应的网址?(网址1切换成网址2,网址需要带引号)

from pybit import spot  # 引入pybit现货

bybit = spot.HTTP(
    endpoint="某网址1",  
    apikey="12345",
    apisecret="67890"
)

我用endpoint=apis
然后用函数控制apis变量为'网址1'或'网址2',
但会报错
raise MissingSchema(
requests.exceptions.MissingSchema: Invalid URL 'dfs/spot/v1/account': No scheme supplied. Perhaps you meant hp://dfs/spot/v1/account?
请问如何解决,十分感谢!

可能是因为你的网址没有添加协议(http 或 https)。

可以在定义网址变量时就添加协议,如:

url1 = 'https://网址1'
url2 = 'https://网址2'

然后在调用 pybit 的 HTTP 函数时使用变量,如:

bybit = spot.HTTP(
    endpoint=url1,  
    apikey="12345",
    apisecret="67890"
)

如果你需要在程序中动态切换网址,可以使用函数来更新 endpoint 变量。

例如,定义一个名为 switch_endpoint 的函数,在函数中更新 endpoint 变量,如:

def switch_endpoint(url):
    bybit.endpoint = url

switch_endpoint(url1)  # 切换到网址1
switch_endpoint(url2)  # 切换到网址2

这样就可以在程序中动态切换网址了。

你的“某网址”有没有加协议?
加上“http:”试试

raise MissingSchema(
requests.exceptions.MissingSchema: Invalid URL 'dfs/spot/v1/account': No scheme supplied. Perhaps you meant hp://dfs/spot/v1/account?
引发缺少架构(
requests.exceptions.MissingSchema:URL“dfs/spot/v1/account”无效:未提供方案。也许你是说hp://dfs/spot/v1/account?
分析:问题的大概意思是无效的url,也就是说这个url是没有效果是错误的,这就就要检查一下url是否符合规定,是否少加了协议
实例:
import requests

a=requests.get(url="www.baidu.com")   #缺少协议,这样请求会报错
print(a.json())

b=requests.get(url="https://www.baidu.com")   #加入协议后就能正确访问
print(b.json())

在 Python 中,你可以使用函数来切换 endpoint 的网址。

一种方法是定义一个函数并将需要切换的 endpoint 网址作为参数传递给函数。在函数中,你可以使用这个网址来更新你程序中需要使用的变量。

下面是一个例子:

def switch_endpoint(new_endpoint):
    global endpoint
    endpoint = new_endpoint

该函数将接收一个新的网址字符串参数new_endpoint, 并将其赋值给endpoint,由于endpoint为全局变量,因此在整个程序中都可以使用,当需要切换网址时,可以直接调用该函数并传入新的网址,就可以实现切换了。

那你其实就可以手写个bybit内置循环了,然后将指定切换你所需要切换的url,尝试这样试试呢?

如果您想要使用函数来动态切换endpoint对应的网址,您需要对程序进行一些修改。
首先,您可以将endpoint参数改为变量,然后在函数中改变这个变量的值。
其次,您需要确保endpoint变量是一个有效的URL。 在您的代码中,endpoint变量是dfs/spot/v1/account,这不是一个有效的URL。

下面是参考代码:

endpoint = "网址1" 

def change_endpoint(new_endpoint):
    endpoint = new_endpoint

bybit = spot.HTTP(
    endpoint=endpoint,  
    apikey="12345",
    apisecret="67890"
)


这样就可以在程序运行过程中通过调用change_endpoint函数来改变endpoint的值。

需要注意的是,您需要确保new_endpoint变量是一个有效的URL。您可以在程序中添加一些验证代码来检查new_endpoint是否是有效的URL。
当然,在您调用change_endpoint函数时,您需要传入一个合法的URL字符串作为参数。 例如:

change_endpoint("https://www.example.com/spot/v1/account")


另外,您可以在函数中添加一些错误检查代码来确保传入的new_endpoint变量是一个有效的URL,如使用urlparse库进行验证,如果不是有效的URL就引发一个异常或者返回错误信息。

from urllib.parse import urlparse

def change_endpoint(new_endpoint):
    try:
        result = urlparse(new_endpoint)
        if all([result.scheme, result.netloc]):
            endpoint = new_endpoint
        else:
            raise ValueError("Invalid URL")
    except ValueError as e:
        print(e)



这样,您就可以使用函数来动态切换endpoint对应的网址了,并且能保证传入的参数是合法的。


SELECT DISTINCT dept FROM T
WHERE 1=1
AND dept <> ''
AND (CASE 
    WHEN user_name = 'A' THEN 1
    WHEN user_name = '其他' THEN COALESCE((SELECT DISTINCT research FROM m WHERE login = user_name), 'not exists') = dept
    ELSE 0 END) = 1

在 SQL 中调整可以使用 CASE 语句来实现不同的逻辑判断。这样,当用户输入参数 A 时,会展示所有 dept;当用户输入其他参数时,会展示对应 dept。
需要注意的是,在使用CASE语句时,需要在最后给出一个ELSE分支。
如果还是有错误,可以先将确定的部分手动运行一下,查看是否有错误,或者将整个语句输出到控制台或者日志文件中,查看具体错误信息。

此外,还需要注意一点,在使用字符串拼接 SQL 语句时,要小心 SQL 注入攻击。需要对用户输入的参数进行过滤和转义。

如果问题依然存在,可以尝试提供详细的错误信息,以及相关的代码和数据结构,以便更好地帮助您解决问题。