Twilio Python - TypeError:需要类似字节的对象,而不是'str'

时间:2017-02-20 09:22:20

标签: python twilio

我正在尝试使用twilio和python发送测试消息。

from twilio.rest import TwilioRestClient

import os
from urllib.parse import urlparse

from twilio.rest.resources import Connection
from twilio.rest.resources.connection import PROXY_TYPE_HTTP

proxy_url = os.environ.get("http_proxy")
host, port = urlparse(proxy_url).netloc.split(":")
Connection.set_proxy_info(host, int(port), proxy_type=PROXY_TYPE_HTTP)


account_sid = "ACd9e7ad451565b50cswfewef" # Your Account SID from    www.twilio.com/console
auth_token  = "dcdasfWEfarewgeargfb"  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(body="Hello body <3?",
to="+65********",    # Replace with your phone number
from_="+1**********") # Replace with your Twilio number

print(message.sid)

我收到此错误:

Traceback (most recent call last):
File "C:\Users\...\Python\SendText.py", line 10, in <module>
host, port = urlparse(proxy_url).netloc.split(":")
TypeError: a bytes-like object is required, not 'str' enter code here

思考:我开始只是从twilio网页上获取示例phython代码,但它似乎不适用于python 3.所以我浏览了互联网并找到了几个要添加的内容 - 我这样做了并且可以减少列表对此的错误(见上文)。在主机中包含 decode(),port = urlparse(proxy_url).netloc.decode()。split(“:”)会删除此错误但同时会产生另一个错误:

Traceback (most recent call last):
File "C:\Users\...\Python\SendText.py", line 10, in <module>
host, port = urlparse(proxy_url).netloc.decode().split(":")
ValueError: not enough values to unpack (expected 2, got 1)

0 个答案:

没有答案