简体   繁体   English

如何修复 HTTP 错误 400:python3 中的错误请求?

[英]How do I fix a HTTP Error 400: Bad Request in python3?

I am trying to access checkpoint firewall by using it's API but for some reason I am getting HTTP Error 400: Bad Request, I have never had this before.我正在尝试通过使用它的 API 来访问检查点防火墙,但由于某种原因,我收到了 HTTP 错误 400:错误请求,我以前从未遇到过这种情况。 Any ideas?有任何想法吗? Here is my code:这是我的代码:

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, '')

basically same thing is working fine with python 2与 python 2 基本相同的事情

this is python2 code这是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, '') 

use this用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM