股票实时数据web接口代码用Python怎么写?

股票实时数据web接口代码用Python怎么写?实际上,一些python http要求库适用最底层TCP/SSL连接的器重。可是,此方法中有一种比较复杂,此外二种也要云服务器适用。我该怎么做?实际上拥有WebSocket,大家完全没必要再进一步了。
最先介绍一下WebSocket。WebSocket是由单一TCP/TSL连接的双工双向通信协议书。WebSocket促使客户端和服务器端的数据传输简单有效,服务器端能够主动向客户端消息推送数据信息。在WebSocket API中,电脑浏览器和服务器之间仅需简易挥手就可以立即创建长久连接,进而开展双重传输数据。
定义很舒服,但还是有一些抽象化。再来看2个典型的例子,让她们迅速了解前面说的具体内容。什么都不说,好好看看编码:

import websocket

import thread

# 在接收到服务器发送消息时调用

def on_message(ws, message):

print('Received: ' + message)

# 在和服务器建立完成连接时调用

def on_open(ws):

# 线程运行函数

def gao():

# 往服务器依次发送 0-4,每次发送完休息 0.1 秒

for i in range(5):

time.sleep(0.01)

msg="{0}".format(i)

ws.send(msg)

print('Sent: ' + msg)

# 休息 1 秒用于接受服务器回复的消息

time.sleep(1)

# 关闭 Websocket 的连接

ws.close()

print("Websocket closed")

# 在另一个线程运行 gao() 函数

thread.start_new_thread(gao, ())

if __name__ == "__main__":

ws = websocket.WebSocketApp("ws://echo.websocket.org/",

on_message = on_message,

on_open = on_open)

ws.run_forever()

#### 输出 #####

Sent: 0

Sent: 1

Received: 0

Sent: 2

Received: 1

Sent: 3

Received: 2

Sent: 4

Received: 3

Received: 4

Websocket closed

你可能感兴趣的:(websocket,网络协议,网络)