【问题标题】:How do I fix a HTTP Error 400: Bad Request in python3?如何修复 HTTP 错误 400:python3 中的错误请求?
【发布时间】:2021-12-26 19:07:35
【问题描述】:

我正在尝试使用它的 API 访问检查点防火墙,但由于某种原因,我收到了 HTTP 错误 400:错误请求,我以前从未遇到过这种情况。 有任何想法吗? 这是我的代码:

import json
import ssl
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
sslctx = ssl.create_default_context()
sslctx.check_hostname = False
sslctx.verify_mode = ssl.CERT_NONE

def checkpoint_api_call(cp_ip, port, command, json_payload, sid):
    apiurl = 'https://' + cp_ip + '/web_api/' + command
    req = urllib.request.Request(apiurl)
    if sid == '':
        req.add_header('Content-Type', 'application/json')
    else:
        req.add_header('Content-Type', 'application/json')
        req.add_header('X-chkp-sid', sid)
    try:
        data1 = urllib.parse.urlencode(json_payload).encode("utf-8")
        resp = urllib.request.urlopen(req, data=data1, context=sslctx)
        res_code = resp.getcode()
        return resp_output, res_code
    except urllib.error.HTTPError as e:
        print((str(e)))
main()
    payload = {'user': admin, 'password': password}
    response, res_code = checkpoint_api_call(x.x.x.x, 443, 'login', payload, '')

基本上同样的事情在 python 2 上也能正常工作

这是python2代码

import ssl
import urllib
import urllib2
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
sslctx = ssl.create_default_context()
sslctx.check_hostname = False
sslctx.verify_mode = ssl.CERT_NONE
def checkpoint_api_call(cp_ip, port, command, json_payload, sid):
    apiurl = 'https://' + cp_ip + '/web_api/' + command
    jencode = json.dumps(json_payload)
    resp_output = ""
    req = urllib2.Request(apiurl)
    if sid == '':
        req.add_header('Content-Type', 'application/json')
    else:
        req.add_header('Content-Type', 'application/json')
        req.add_header('X-chkp-sid', sid)
    try:
        resp = urllib2.urlopen(req, data=jencode, context=sslctx)
        res_code = resp.getcode()
        resp_output = json.load(resp)
        return resp_output, res_code
    except urllib2.HTTPError as e:
        res_code = e.getcode()
        print e.getcode()
        return resp_output, res_code

main()
    payload = {'user': user, 'password': password}
    response, res_code = checkpoint_api_call(x.x.x.x, 443, 'login', payload, '') 

【问题讨论】:

  • 400 是最常见的 HTTP 错误,您希望我们如何从中看出什么?至少有任何防火墙 API 参考、响应负载中的任何内容吗?
  • 如果我没记错的话,其中一些防火墙需要启用来自您的 IP 地址的连接。您是否已启用管理服务器以接受来自您的 IP 的连接?
  • @EyalGolan 基本上同样的事情在 python 2 上运行良好我也更新了 python 2 代码
  • @bereal 我也更新了添加的 python2 代码
  • @KshitijGupta 在您的工作代码中发送 json,在 python 3 代码中您 urlencode 它。不一样。

标签: python python-3.x http-status-code-400 bad-request checkpoint


【解决方案1】:

使用这个

try:
    jencode = json.dumps(json_payload)
    jencode = jencode.encode("utf-8")
    resp = urllib.request.urlopen(req, data=jencode, context=sslctx)
    res_code = resp.getcode()
    resp_output = json.load(resp)
    return resp_output, res_code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2023-03-08
    • 2012-02-09
    相关资源
    最近更新 更多