Solution for getting a host to send an HTTP request message

Author: The Little Dream, Created: 2020-09-23 16:24:42, Updated: 2023-09-27 19:37:55

img

Solution for getting a host to send an HTTP request message

When testing, debugging policy code and running a real-time robot, there are often cases of exchange interface error reports, at this time to consult the exchange interface API documentation, to query the relevant error information, consult the exchange API technical customer service always need to provide an error request report, to analyze the cause of the error.

In this article, we'll explore two possible solutions.

1, Printing a request message sent using Python's Scapy library scraper

Installing firstscapyModule

pip3 install scapy 

Then create a Python policy:

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")

Then create a bot that uses this policy, which will grab the http packet from the host's server (https can't grab some processing for this).

Run this bot and you can use the debugger to send a request and have the bot grab the package. In the debugger we write the code to send the request.

function main(){
    // 要把基地址设置为其它http协议的地址,如果不设置交易所的地址一般都是https,这样是抓不到包的
    exchange.SetBase("http://www.baidu.com")    
    
    // POST 请求
    exchange.IO("api", "POST", "/api/swap/v3/order", "aaa=111&bbb=222")
    
    // GET 请求
    exchange.SetContractType("swap")
    exchange.GetTicker()
}

The message printed by the robot:img

We can copy and read the article: GET requested a report:

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 

Host: www.baidu.comI'm not saying that we can't get our hands on the packaging, that we can ignore it, that the right thing to do isHost: www.okex.comYou can see the link in the petition:/api/swap/v3/instruments/BTC-USD-SWAP/tickerIn the meantime, we've got to get some data on the market for BTC.

POST request for a report:

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, like Gecko) Chrome/35.0.1916.153 Safari/537.36 
Content-Length: 25 
Content-Type: application/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 Accept-Encoding: gzip 

{"aaa":"111","bbb":"222"}

You can see the request path is:/api/swap/v3/orderI'm not sure. Access key verified:d487230f-ccccc-aaaaa-bbbbb-268fef99cfe4(Demonstration, not real KEY) Sign the petition:h1x6f80rhhkELobJcO1rFyMgUUshOlmgjRBHD+pLvG0=The following is a list of the keywords that are used in the API KEY password:abc123(Demonstration used) Body data requested:{"aaa":"111","bbb":"222"}

This allows you to observe the request message, analyze the reason for the interface request.

2 Local request for interception

The second method, without creating a robot, is to use Apple's Mac self-driving car.Netcat : https://baike.baidu.com/item/Netcat/9952751?fr=aladdin◦ Listening to requests and printing news.

In the terminal, use commandsnc -l 8080It's called Netcat.

Here is a picture:img

Similarly, we deploy a host on the local machine, and then in the debug tool, use the following code to send a request.

function main(){
    exchange.SetBase("http://127.0.0.1:8080")    // 这里把基地址改为本机,端口8080,Netcat 就可以监听到请求了
    // POST 请求
    exchange.IO("api", "POST", "/api/swap/v3/order", "aaa=111&bbb=222")
    
    // GET 请求
    exchange.SetContractType("swap")
    exchange.GetTicker()
}

The POST request message printed on the terminal:img

The GET request message printed on the terminal:img


Related

More