Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无效的源标题_Python_Websocket - Fatal编程技术网

Python 无效的源标题

Python 无效的源标题,python,websocket,Python,Websocket,我正在尝试建立websocket连接,但出现错误: 请求标头中的来源不正确 我的代码是: def create_signature(): timestamp = int(datetime.datetime.now().timestamp()) string = "{}{}".format(timestamp, key) return timestamp, hmac.new(secret.encode(), string.encode(), hashlib.sha256)

我正在尝试建立websocket连接,但出现错误:

请求标头中的来源不正确

我的代码是:

def create_signature():
    timestamp = int(datetime.datetime.now().timestamp()) 
    string = "{}{}".format(timestamp, key)
    return timestamp, hmac.new(secret.encode(), string.encode(), hashlib.sha256).hexdigest()
def auth_request():
    timestamp, signature = create_signature()
    return json.dumps({'e': 'auth',
        'auth': {'key': key, 'signature': signature, 'timestamp': timestamp}})

def main():
    ws = create_connection(websocketURL)
    authentication = auth_request()
    ws.send(authentication)
    result = ws.recv()
    print(result)
    ws.close()
我正在运行
main
函数
auth_请求
返回有效的json。我正在尝试获取有效的响应对象,但连接失败。我已经导入了相关的库,没有任何语法错误。所有变量都在另一个文件中定义


如何解决这个问题,这是我第一次使用web套接字

我相信您的代码来自www.cex.io。(如果不是,对不起)

为了使用cex.io ws-api,您需要在连接到websocket时声明正确的原始标头:
来源:wss.cex.io
(我个人向cex.io支持团队提出了这个问题)

对于市场数据(股票代码、ohlcv…),您不需要验证
https://gist.github.com/icehongssii/1c8ec1755dfee88c2789714ee9a37738.js

代码本身可能没有问题-问题在于请求源标头和类似CSRF/CORS的行为-即服务器拒绝来自不同/不受信任域的请求。有关这种检查的http实现的一些背景信息,请参阅和。。为什么这是http CORS问题,web套接字使用TCP而不是http的全部目的不是吗?@WMios:No;这将破坏整个网络安全模型。Web套接字是一种类似HTTP的协议,具有类似的限制。@WMios:我相信它可能不会作为CORS实现,但SERVICES可以查看来源并选择拒绝来自不同域的协议。将更新评论以澄清。@SLaks,啊,谢谢,我没有意识到!您如何在python中实现websocket头文件?不再在项目中工作,但谢谢-这是为了cex。6个月前有非常糟糕的文档。@WMios是的,我完全同意他们有如此糟糕的文档。他们的支持也没有那么大帮助。即使我指出他们的代码,他们也没有修复它。