简体   繁体   English

尝试在python中使用HTTP POST请求进行POST

[英]Trying to POST using a HTTP POST-request in python

I am trying to post two parameters using HTTP POST-request to http://foo.bar.com/submit.jsp . 我正在尝试使用HTTP POST请求将两个参数发布到http://foo.bar.com/submit.jsp

This works. 这可行。

conn = httplib.HTTPConnection("www.bar.com")
conn.request("POST", "",parameters)

But this does not, could someone explain to me why it does not and how the correct way is? 但这不是,有人可以向我解释为什么不这样做,以及正确的方法是什么?

conn = httplib.HTTPConnection("www.foo.bar.com")
conn.request("POST", "/submit.jsp", parameters)

I am getting this error 我收到此错误

File "example.py", line 68, in <module>
    conn.request("POST", "/submit.jsp", parameters)
  File "/usr/lib/python2.7/httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 776, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

Thanks in advance 提前致谢

Whatever hostname you are really using in place of www.foo.bar.com can not be resolved to an IP address. 无论您实际使用什么主机名代替www.foo.bar.com ,都不能将其解析为IP地址。 Note that your first example used www.bar.com , not www.foo.bar.com . 请注意,您的第一个示例使用了www.bar.com ,而不是www.foo.bar.com While these are only examples, the second hostname that you are using is bad. 虽然这些只是示例,但您使用的第二个主机名是错误的。

The path /submit.jsp is not the problem here, if that is what you are actually trying to test? 路径/submit.jsp在这里不是问题,如果那是您实际上要测试的内容?

This error means that python cannot reach the HTTP server you defined in the function, You could have a unneeded "HTTP://", A typo'd url, if you're using IPV6 it could be the form of your address or other network issues. 此错误意味着python无法访问您在函数中定义的HTTP服务器。您可能不需要使用“ HTTP://”(输入错误的网址),如果您使用的是IPV6,则可以是您的地址或其他形式网络问题。

This might help; 这可能会有所帮助;

Error while using conn = httplib.HTTPConnection ("http://ipaddr:port") 使用conn = httplib.HTTPConnection(“ http:// ipaddr:port”)时出错

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

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