Вывод на печать в скрипте curl - Python - PullRequest
0 голосов
/ 27 марта 2020

нужна помощь с этим. Я уже создал итерационный скрипт в Python для выполнения завитка по одному и тому же URL, но с несколькими PORTS, но я борюсь с желаемым выводом.

Я получаю 1 из этого за каждый атм PORT.

<!-- current output -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

Мне нужно максимально очистить вывод, так как меня интересует только печать URL + PORT и HTTP_STATUS_CODE при каждом выполнении. , Или что-то похожее на это:

<!-- desired output
CURL_AT=http://www.isuckatpython/shootmyself.com:8080
HTTP_STATUS_CODE=411 -->

Это мой текущий сценарий

import subprocess
import os

port_list = [8080, 8090, 8091, 8092, 8093, 8094, 8095]
for i in port_list:
    i = str(i)
    subprocess.call(["curl", "-s", "-X", "POST", "http://www.isuckatpython/shootmyself.com:", i])

Надеюсь, я был достаточно ясен.

С уважением, Алем.

1 Ответ

0 голосов
/ 27 марта 2020

Таким способом вы можете получить ожидаемый результат.

import subprocess


port_list = [8080, 8090, 8091, 8092, 8093, 8094, 8095]
host = "http://www.isuckatpython/shootmyself.com:"

for port in port_list:
    result = subprocess.Popen('curl -s -o /dev/null -w "%{http_code}" '+ host + str(port),
                              stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    output, error = result.communicate()
    output = output.decode('utf-8')
    if output != '000':
        # if curl:  Failed to connect to <url> port gives "000"
        print("CURL_AT="+host+str(port))
        print("HTTP_STATUS_CODE="+output)
    else:
        print("curl: Failed to connect to {0} port {1}: Connection ".format(host, port))
...