【问题标题】:Python error when sending POST - a bytes-like object is required, not 'str'发送 POST 时出现 Python 错误 - 需要类似字节的对象,而不是“str”
【发布时间】:2021-11-04 15:26:05
【问题描述】:

我正在编写一个脚本,通过获取开放端口 POST 请求并在此处使用它来打开路由器/调制解调器上的端口,但发送时出现此错误:TypeError: a bytes-like object is required, not 'str' .它应该只发送 POST 请求并打开端口而不是错误。

    url = "http://192.168.0.1/apply_abstract.cgi" # http://192.168.0.1/wan_portforwarding.htm?m=adv
    reqInfo = {
        'action: ui_firewall',
        'httoken: REMOVED FOR SAFETY',
        'submit_button: wan_portforwarding.htm',
        '786434001000: ' + GlobalForwardingName,
        '786433001000: 1',
        '786435001000: ' + GlobalInternalIP,
        '#786437001000: ' + GlobalProtocol,
        '786438001000: ', GlobalLanPort,
        '786439001000: ', GlobalLanPort,
        '786440001000: ', GlobalWanPort,
        '786441001000: ', GlobalWanPort
    }

    sendReq = requests.post(url, data = reqInfo)```

【问题讨论】:

标签: python post networking router modem


【解决方案1】:

您没有发送 dict 对象,只有字符串,这可能是主要问题,试试这个:

reqInfo = {
    'action': ui_firewall,
    'httoken': 'REMOVED FOR SAFETY',
    'submit_button': wan_portforwarding,
    '786434001000':   GlobalForwardingName,
    '786433001000': 1,
    '786435001000':  GlobalInternalIP,
    '#786437001000': GlobalProtocol,
    '786438001000':  GlobalLanPort,
    '786439001000':  GlobalLanPort,
    '786440001000':  GlobalWanPort,
    '786441001000':  GlobalWanPort
}

sendReq = requests.post(url, data = reqInfo)

查看请求文档:https://docs.python-requests.org/en/master/user/quickstart/

【讨论】:

  • 没有错误,但列表中没有新的端口转发。
  • 不要认为它会起作用,因为 HTTOKEN 会更改每个 POST
  • 您不能在 HTTOKEN 上使用变量读取实际令牌并使用 str() 函数对其进行解析
  • 好的,我试过你的解决方案没用,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多