Saat menguji dan men-debug kode strategi, atau menjalankan bot di pasar nyata, antarmuka platform sering dilaporkan dengan kesalahan. Pada saat ini, Anda perlu menanyakan dokumentasi API antarmuka platform, mencari informasi pelaporan kesalahan yang relevan, dan selalu perlu memberikan pesan permintaan kesalahan, ketika menanyakan layanan teknis API platform, untuk menganalisis penyebab kesalahan.
Jika Anda tidak dapat melihat informasi pesan, akan sulit untuk menemukan masalah.
Pertama, installscapy
.
pip3 install scapy
Kemudian, buatlah strategi Python:
from scapy.all import *
def Method_print(packet):
ret = "\n".join(packet.sprintf("{Raw:%Raw.load%}").split(r"\r\n"))
Log(ret)
sniff(
iface='eth0',
prn=Method_print,
lfilter=lambda p: "GET" in str(p) or "POST" in str(p),
filter="tcp")
Kemudian buat bot yang menggunakan strategi, dan bot akan menangkap paket http yang dikirim oleh server docker (https tidak dapat menangkap paket, dan kami memiliki beberapa pemrosesan untuk itu).
Jalankan bot menangkap paket, dan kemudian Anda dapat menggunakan alat debugging untuk mengirim permintaan untuk membiarkan bot menangkap paket.
function main(){
// The base address should be set to the address of other http protocols. If the address of a platform is not set, it is generally https, so the packet cannot be captured.
exchange.SetBase("http://www.baidu.com")
// POST request
exchange.IO("api", "POST", "/api/swap/v3/order", "aaa=111&bbb=222")
// GET request
exchange.SetContractType("swap")
exchange.GetTicker()
}
Informasi yang dicetak oleh bot menangkap paket:
Kita bisa menyalin pesan permintaan dan melihat: Pesan permintaan GET:
GET
/api/swap/v3/instruments/BTC-USD-SWAP/ticker
HTTP/1.1
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 Accept-Encoding: gzip
You can see the link in the request message is: ```/api/swap/v3/instruments/BTC-USD-SWAP/ticker```, which is to request the crypto-margined (BTC) perpetual contract market data.
POST request message:
POST /api/swap/v3/order HTTP/1.1 Host: www.baidu.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, seperti Gecko) Chrome/35.0.1916.153 Safari/537.36 Panjang isi: 25 Content-Type: aplikasi/json; Charset=UTF-8 OK-Access-Key: d487230f-ccccc-aaaaa-bbbbb-268fef99cfe4 Ok-Access-Passphrase: abc123 OK-Access-Sign: h1x6f80rhhkELobJcO1rFyMgUUshOlmgjRBHD+pLvG0= Ok-Access-Timestamp: 2020-09-23T08:43:49.906Z Terima-Enkoding: gzip
{Y: i: i: i: i: i: i}
You can see the request path is: ```/api/swap/v3/order```.
Verified Access key: ```d487230f-ccccc-aaaaa-bbbbb-268fef99cfe4``` (for demo, not real KEY)
Signature of this request: ```h1x6f80rhhkELobJcO1rFyMgUUshOlmgjRBHD+pLvG0=```
API KEY Passphrase: ```abc123``` (for demo)
Requested Body data: ```{"aaa":"111","bbb":"222"}```.
Thus, we can observe the request messages, and analyze the causes of errors encountered by the interface.
### 2. Local Listener Request
The second solution, without creating a bot, is to use the ```Netcat``` that comes with the Mac system: https://baike.baidu.com/item/Netcat/9952751?fr=aladdin. Listen to requests and print messages.
In the terminal, use the command ```nc -l 8080``` to run Netcat.
As is shown in the picture:
![Solutions to Obtaining Docker Http Request Message](/upload/asset/16ea458dfeb3d64ea2e9.png)
Similarly, we deploy a docker on this machine, and then in the debugging tool, use the following code to send a request.
fungsi utama
exchange.SetBase(
// GET request
exchange.SetContractType("swap")
exchange.GetTicker()
} “`
POST pesan permintaan dicetak di terminal:
Pesan permintaan GET dicetak di terminal: