【问题标题】:How to detect GraphQL endpoint using Python如何使用 Python 检测 GraphQL 端点
【发布时间】:2020-10-01 07:13:48
【问题描述】:

我正在尝试使用 Python 语言检测 graphql 端点。我是一个绝对的初学者,但我尝试过编写代码。 你能建议改变和更好的方法吗? 代码:

import requests,urllib,urllib.request
import string
consoleDict = [
    "",
    "/graphql",
    "/graphql/console",
    "graphql.php",
    "graphiql",
    "explorer",
    "altair",
    "/playground"
          ]
for endpoint in consoleDict:
    ep = ' http://159.100.248.211 '
    response = requests.get(ep)
    if response.status_code in [200,403]:
        print("It is a GraphQL endpoint",endpoint)

谢谢你:)

【问题讨论】:

  • 您可能想要使用 graphql 客户端库,例如 gql
  • 嘿,你能告诉我怎么做吗,也许可以帮我写代码。我绝对是初学者,所以:3
  • 您根本没有使用 endpoint 变量。然后你总是向同一个主机 http:159.100.248.211 发出请求。

标签: python python-requests graphql endpoint graphql-python


【解决方案1】:

即使使用gql,您也需要架构来请求任何内容。如果你不知道,你可以使用自省查询:

{
  __schema {
    types {
      name
    }
  }
}

某些端点可能已禁用此功能,但如果您不知道架构,这是一个很好的起点。 试试这样的:

import json
import requests
from urllib import parse

paths = [
    "",
    "/graphql",
    "/graphql/console",
    "graphql.php",
    "graphiql",
    "explorer",
    "altair",
    "/playground"
]

query = """{
  __schema {
    types {
      name
    }
  }
}
"""

for path in paths:
    hostname = 'http://159.100.248.211'
    endpoint = parse.urljoin(hostname, path)
    try:
        print(f"Attempt: {endpoint}")
        response = requests.post(endpoint, json={'query': query}, timeout=0.1)
    except Exception:
        print("No GraphQL endpoint found")
    else:
        if response.status_code == 200:
            json_data = json.loads(response.text)
            if json_data.get('data'):
                print("It is a GraphQL endpoint",endpoint)

让 mw 知道这是否可行

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 2020-05-16
    • 2017-10-31
    • 2020-09-03
    • 2022-01-19
    • 2021-04-16
    • 2020-10-06
    • 2018-12-14
    • 2020-09-07
    相关资源
    最近更新 更多