使用python 2.6.6和httplib发送的头文件不正确

使用python 2.6.6和httplib发送的头文件不正确,python,httplib,Python,Httplib,我有一个非常基本的python脚本,我正在使用它来实现特定的端点。由于某些原因,我无法让它发送我需要在特定服务器上发送的授权标头。下面的信息是虚构的,但却是我正在做的一个很好的例子。我编写了这个脚本,它在我的虚拟机上运行良好,但在实际服务器上运行不好。Python版本都是2.6.6。我无法更改版本,因此请不要建议更新。我写这个问题是为了获得指导,了解为什么标题可能会被删除 这些环境完全相同,并使用配置管理器来保持其设置的一致性 这是剧本。非常直截了当。我只是做了一个API调用来返回一个基于访问令

我有一个非常基本的python脚本,我正在使用它来实现特定的端点。由于某些原因,我无法让它发送我需要在特定服务器上发送的授权标头。下面的信息是虚构的,但却是我正在做的一个很好的例子。我编写了这个脚本,它在我的虚拟机上运行良好,但在实际服务器上运行不好。Python版本都是2.6.6。我无法更改版本,因此请不要建议更新。我写这个问题是为了获得指导,了解为什么标题可能会被删除

这些环境完全相同,并使用配置管理器来保持其设置的一致性

这是剧本。非常直截了当。我只是做了一个API调用来返回一个基于访问令牌的用户模型

#! /usr/bin/env python
import httplib, os, socket

conn = httplib.HTTPConnection(socket.gethostname())
conn.set_debuglevel(1)
conn.putrequest("GET", "/api/index.php/user")
conn.putheader('Authorization','Bearer 123456')
conn.endheaders()
response = conn.getresponse()

print response.status
print response.reason
print response.read()
在我的虚拟机上请求和调试信息

[root@vm scripts]# ./test.py 
send: 'GET /api/index.php/user 
    HTTP/1.1\r\n
    Host: vm.shibby.com\r\n
    Accept-Encoding: identity\r\n
    Authorization: Bearer 123456\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Mon, 10 Mar 2014 22:18:00 GMT
header: Server: Apache/2.2.15 (CentOS)
header: X-Powered-By: PHP/5.3.3
header: X-Frame-Options: SAMEORIGIN, SAMEORIGIN
header: Connection: close
header: Transfer-Encoding: chunked
header: Content-Type: application/json; charset=utf-8
200
OK
VM收到的请求-转储到日志中

2014-03-10 22:18:00 --- DEBUG: headers: HTTP_Header Object
(
    [_accept_content:protected] => 
    [_accept_charset:protected] => 
    [_accept_encoding:protected] => 
    [_accept_language:protected] => 
    [storage:ArrayObject:private] => Array
    (
        [authorization] => Bearer 123456
        [host] => vm.shibby.com
        [accept-encoding] => identity
        [connection] => close
    )
)
在我的服务器上输出

[root@vm scripts]# ./test.py
send: 'GET /api/index.php/user 
    HTTP/1.1\r\n
    Host: shibby.com\r\n
    Accept-Encoding: identity\r\n
    Authorization: Bearer 123456\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Date: Mon, 10 Mar 2014 22:24:55 GMT
header: Server: Apache
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Headers: Authorization
header: X-Frame-Options: SAMEORIGIN
header: Content-Length: 37
header: Connection: close
header: Content-Type: application/json
400
Bad Request
{"error":"invalid_token"}
2014-03-10 22:33:23 --- DEBUG: headers: HTTP_Header Object
(
    [_accept_content:protected] => 
    [_accept_charset:protected] => 
    [_accept_encoding:protected] => 
    [_accept_language:protected] => 
    [storage:ArrayObject:private] => Array
        (
            [host] => shibby.com
            [accept-encoding] => identity
            [connection] => close
        )

)
服务器收到的请求

[root@vm scripts]# ./test.py
send: 'GET /api/index.php/user 
    HTTP/1.1\r\n
    Host: shibby.com\r\n
    Accept-Encoding: identity\r\n
    Authorization: Bearer 123456\r\n\r\n'
reply: 'HTTP/1.1 400 Bad Request\r\n'
header: Date: Mon, 10 Mar 2014 22:24:55 GMT
header: Server: Apache
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Headers: Authorization
header: X-Frame-Options: SAMEORIGIN
header: Content-Length: 37
header: Connection: close
header: Content-Type: application/json
400
Bad Request
{"error":"invalid_token"}
2014-03-10 22:33:23 --- DEBUG: headers: HTTP_Header Object
(
    [_accept_content:protected] => 
    [_accept_charset:protected] => 
    [_accept_encoding:protected] => 
    [_accept_language:protected] => 
    [storage:ArrayObject:private] => Array
        (
            [host] => shibby.com
            [accept-encoding] => identity
            [connection] => close
        )

)

如您所见,相关端点未收到授权标头。此时没有处理,我正在登录控制器的构造函数。

好的,我遗漏了一些重要信息。我使用的框架是Kohana3.2,显然它去掉了授权头。我通过添加一行代码将其设置为apache中的环境变量来解决这个问题。看起来没有运行它,因为我没有以子域的形式访问端点。无论如何,希望这能帮助其他人