【问题标题】:python variable empty on concat after response.getvalue()在response.getvalue()之后concat上的python变量为空
【发布时间】:2015-06-02 19:12:01
【问题描述】:

(python新手)

我正在尝试对文件进行简单的经过身份验证的放置...所以我制作了两个卷曲,第一个进行身份验证(按预期打印令牌)但是当我使用相同的变量(令牌)来将其添加到标头 ("Authorization: Bearer %s" % str(token)) 令牌为空。我在这里做错了什么?

import urllib
import cStringIO
import pycurl
import requests
from urllib import urlencode
import os.path

# declarations
filename = "./profile.jpg"
response = cStringIO.StringIO()
c = pycurl.Curl()

# formdata
post_data = {'username': '...', 'password':'...'}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.

print "*****************************************************"

# authenticate
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.URL, "https://.../auth")
c.setopt(c.POSTFIELDS, postfields)
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.SSL_VERIFYHOST, 0)
c.setopt(c.VERBOSE, 1)
c.perform()
c.close() 
token = response.getvalue()
print token

print "*****************************************************"

# upload file
filesize = os.path.getsize(filename)
fin = open(filename, 'rb')

c = pycurl.Curl()
c.setopt(c.PUT, 1)
c.setopt(c.URL, "https://.../avatar")
c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, filename))])
c.setopt(c.HTTPHEADER, [
    "Authorization: Bearer %s" % str(token), 
    "Content-Type: image/jpeg"
])
c.setopt(c.READFUNCTION, fin.read)
c.setopt(c.POSTFIELDSIZE, filesize)
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.SSL_VERIFYHOST, 0)
c.setopt(c.VERBOSE, 1)
c.setopt(c.WRITEFUNCTION, response.write),
c.perform()
c.close() 
print response.getvalue()

print "*****************************************************"

请求:

> PUT ../avatar HTTP/1.1
User-Agent: PycURL/7.19.3 libcurl/7.35.0 GnuTLS/2.12.23 zlib/1.2.8 libidn/1.28 librtmp/2.3
Host: 127.0.0.1:8080
Accept: */*
Transfer-Encoding: chunked
Authorization: Bearer 
Expect: 100-continue

回复:

< HTTP/1.1 100 Continue
< HTTP/1.1 401 Unauthorized
< content-type: application/json; charset=utf-8
< cache-control: no-cache
< content-length: 86
< Date: Tue, 02 Jun 2015 19:09:29 GMT
< Connection: keep-alive
< 
* Connection #1 to host 127.0.0.1 left intact

{"statusCode":401,"error":"Unauthorized","message":"Incorrect Token or Token Expired"}

【问题讨论】:

  • 你得到的确切错误是什么?
  • 401 未授权...但输出标头显示:...授权:承载...标头中没有令牌
  • @James - 这实际上不是错误 - 如果没有授权标头中的令牌,我会期望 401。问题是令牌似乎是空的,但是,在 print token 行令牌正在打印到控制台。

标签: python file-upload pycurl


【解决方案1】:

我认为存在编码问题。 print 函数能够在不关心编码的情况下输出一些东西。查看PycURL quickstart documentation 它提到了这个问题。我会尝试操纵这一行的编码:

"Authorization: Bearer %s" % str(token)

并尝试做这样的事情:

"Authorization: Bearer %s" % token.decode('iso-8859-1')

(我也会尝试.decode("utf-8"),这取决于编码是什么)

您可能需要将response = cStringIO.StringIO() 更改为response = BytesIO()。我无法给出明确的答案,因为我不确定您的设置。

编辑this post about cStringIO 证实了我对编码的怀疑,它说不支持 Unicode。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2015-02-07
    • 2021-12-11
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多