python http服务器_Python连接到HTTP服务器

In my program, I am trying to access https://api.dropbox.com/1/oauth2/token. In order to do that, I was trying to use http.client.HTTPSConnection(). However, I am receiving a 400 statement from the server, even though when I send the same request through my browser, I get an actual response:

{"error": "Call requires one of the following methods: POST, OPTIONS. Got GET."}

I believe that this happens for subdomains, since I also tested the function for https://docs.python.org/3/, and the result is very similar.

Here is my code (Python3):

conn = http.client.HTTPSConnection('docs.python.org')

conn.request('get', '/3/')

response = conn.getresponse().read()

print(response)

How should I use the http.client library to send the proper request?

解决方案

TL;DR: Change the lowercase 'get' to uppercase 'GET' should resolve the problem.

The reason: according to section 5.1.1, RFC2616:

The Method token indicates the method to be performed on the

resource identified by the Request-URI. The method is case-sensitive.

RFC2616 also defined 8 methods which are "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", and "CONNECT". All of them are uppercase.

We do know some HTTP clients like python-requests and jQuery.ajax also support lowercase methods but they are just not the standard way defined by the RFC for using those methods. To prevent issues, use uppercase ones first.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值