我试图将名称模块与temp-mail.org(https://github.com/saippuakauppias/temp-mail)的包装一起用于涉及创建临时电子邮件地址的项目的一部分。我的代码如下所示,除非n大于2,否则工作正常。
import names
from random import randint
from tempmail import TempMail
n = input("Please enter number of email addresses: ")
i = 0
while i < n:
first = names.get_first_name()
second = names.get_last_name()
tm = TempMail(login= first + "." + second + str(randint(1,99)))
email = tm.get_email_address()
print email
i = i+1
如果n大于2,则循环将执行两次,然后我收到以下错误:
>>> runfile('/root/voter.py', wdir='/root')
Please enter number of email addresses: 3
Randell.Elkin17@stexsy.com
Michael.Martinez6@stexsy.com
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/root/voter.py", line 12, in <module>
email = tm.get_email_address()
File "/usr/local/lib/python2.7/dist-packages/tempmail.py", line 66, in get_email_address
available_domains = self.available_domains
File "/usr/local/lib/python2.7/dist-packages/tempmail.py", line 36, in available_domains
domains = req.json()
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 826, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我知道这是一个远景,因为tempmail模块并没有被广泛使用,但是我希望我在其他地方犯了一个简单的错误,有人可以提供帮助吗?感谢
答案 0 :(得分:0)
好的,所以我查看了你的问题。
软件包temp-mail似乎向您调用TempMail()
的每个时间提出temp-mail.ru 请求,以获取可用域列表。
您遇到了API的rate limiting(简而言之,您在太短的时间内调用了API太多次)
我在包中添加了一些调试以查看发生了什么。我创建了一个virtualenv来测试您的代码。这使我安装的模块(temp-mail
和names
仅在项目本地而不是全局安装在系统上)并查看堆栈跟踪。调用json部分的行是domains = req.json()
,位于/home/arch/tmp-tempmail/env/lib/python2.7/site-packages/tempmail.py
。
然后我编辑了文件以添加一些打印件(您必须首先了解request模块的一些概念):
url = 'http://{0}/request/domains/format/json/'.format(
self.api_domain)
req = requests.get(url)
print req
print req.text
domains = req.json()
输出结果为:
Please enter number of email addresses: 3
<Response [200]>
["@stexsy.com"]
Larry.Gerald20@stexsy.com
<Response [200]>
["@stexsy.com"]
Darlene.Bullock32@stexsy.com
<Response [429]>
Traceback (most recent call last):
File "main.py", line 29, in <module>
email = tm.get_email_address()
File "/home/arch/tmp-tempmail/env/lib/python2.7/site-packages/tempmail.py", line 68, in get_email_address
available_domains = self.available_domains
File "/home/arch/tmp-tempmail/env/lib/python2.7/site-packages/tempmail.py", line 38, in available_domains
domains = req.json()
File "/home/arch/tmp-tempmail/env/lib/python2.7/site-packages/requests/models.py", line 826, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python2.7/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
你可以看到失败的响应是第三个,它有HTTP错误代码429,转换为“Too Many Requests”。 JSON解析器因为输入为空而崩溃。
另一方面,API的docs表示:
您可以在没有API通知的情况下生成绝对任何电子邮件地址
因此,如果您拥有该域,则无需向服务器发出请求以在服务器上接收邮件。
您还可以在循环内添加延迟。
import names
from random import randint
from tempmail import TempMail
import time
n = input("Please enter number of email addresses: ")
i = 0
while i < n:
first = names.get_first_name()
second = names.get_last_name()
tm = TempMail(login= first + "." + second + str(randint(1,99)))
email = tm.get_email_address()
print email
time.sleep(1)
i = i+1