- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我用 Python 编写了我的第一个程序。
#This program casts votes in online poll using different proxy servers for each request.
#It works, but some proxy servers cause errors crashing the whole thing.
#To avoid that, I would like it to skip those servers and ignore the errors.
import requests
import time
#Votes to be cast
votes = 5
#Makes proxy list
f=open('proxy2.txt')
lines=f.read().splitlines()
f.close()
#Vote counter
i = 1
#Proxy list counter
j = 0
while (i<=votes):
#Tests and moves to next proxy if there was a problem.
try:
r = requests.get('http://www.google.com')
except requests.exceptions.RequestException:
j = j + 1
#Headers copied from my browser. Some of them cause errors. Could you tell me why?
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
#'Accept-Encoding': 'gzip, deflate',
#'Accept-Language': 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
#'Cache-Control': 'max-age=0',
#'Connection': 'keep-alive',
#'Content-Length': '101',
'Content-Type': 'application/x-www-form-urlencoded',
#'Host': 'www.mylomza.pl',
#'Origin': 'http://www.mylomza.pl',
#'Referer': 'http://www.mylomza.pl/home/lomza/item/11780-wybierz-miss-%C5%82ks-i-portalu-mylomzapl-video-i-foto.html',
#'Upgrade-Insecure-Requests': '1',
#'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
}
proxies = {
'http': 'http://'+lines[j] #31.207.0.99:3128
}
r = requests.get('http://www.mylomza.pl/home/lomza/item/11780-wybierz-miss-%C5%82ks-i-portalu-mylomzapl-video-i-foto.html', headers=headers, proxies=proxies, timeout=10)
#The funny part - form, that I have to post, requires some kind of ID and this is my way of getting it :P Feel free to suggest an alternative way.
userid = r.text[(22222-32):22222]
print('Voter', userid, 'registered.')
data = {
'voteid': '141',
'task_button': 'Głosuj',
'option': 'com_poll',
'task': 'vote',
'id': '25',
userid: '1'
}
r = requests.post('http://www.mylomza.pl/home/lomza/item/index.php', headers=headers, cookies=r.cookies, data=data, proxies=proxies, timeout=10)
print('Vote nr', i, 'cast from', lines[i])
i = i + 1
j = j + 1
time.sleep(1)
我需要的是让它处理异常和错误。
#Tests and moves to next proxy if there was a problem.
try:
r = requests.get('http://www.google.com')
except requests.exceptions.RequestException:
j = j + 1
除此之外,我还可以使用另一种方法来实现这一目标:
#The funny part - form, that I have to post, requires some kind of ID and this is my way of getting it :P Feel free to suggest an alternative way.
userid = r.text[(22222-32):22222]
有时我的方法不起作用(如下例)。第一次投票通过,第二次没有通过,然后全部崩溃。
Voter 53bf55490ebd07d9c190787c5c6ca44c registered.
Vote nr 1 cast from 111.23.6.161:80
Voter registered.
Vote nr 2 cast from 94.141.102.203:8080
Traceback (most recent call last):
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connection.py", line 142, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\util\connection.py", line 91, in create_connection
raise err
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\util\connection.py", line 81, in create_connection
sock.connect(sa)
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 578, in urlopen
chunked=chunked)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 362, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1128, in _send_request
self.endheaders(body)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1079, in endheaders
self._send_output(message_body)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 911, in _send_output
self.send(msg)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 854, in send
self.connect()
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connection.py", line 167, in connect
conn = self._new_conn()
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connection.py", line 147, in _new_conn
(self.host, self.timeout))
requests.packages.urllib3.exceptions.ConnectTimeoutError: (<requests.packages.urllib3.connection.HTTPConnection object at 0x03612730>, 'Connection to 94.141.102.203 timed out. (connect timeout=10)')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\adapters.py", line 403, in send
timeout=timeout
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 623, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\packages\urllib3\util\retry.py", line 281, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='94.141.102.203', port=8080): Max retries exceeded with url: http://www.mylomza.pl/home/lomza/item/11780-wybierz-miss-%C5%82ks-i-portalu-mylomzapl-video-i-foto.html (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x03612730>, 'Connection to 94.141.102.203 timed out. (connect timeout=10)'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\PollVoter.py", line 50, in <module>
r = requests.get('http://www.mylomza.pl/home/lomza/item/11780-wybierz-miss-%C5%82ks-i-portalu-mylomzapl-video-i-foto.html', headers=headers, proxies=proxies, timeout=10)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\api.py", line 71, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\api.py", line 57, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Adrian\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\adapters.py", line 459, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='94.141.102.203', port=8080): Max retries exceeded with url: http://www.mylomza.pl/home/lomza/item/11780-wybierz-miss-%C5%82ks-i-portalu-mylomzapl-video-i-foto.html (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x03612730>, 'Connection to 94.141.102.203 timed out. (connect timeout=10)'))
最佳答案
看起来您正在用太多请求淹没服务器,这就是为什么您会收到其他错误,例如 requests.packages.urllib3.exceptions.MaxRetryError
,因为服务器可能会限制您在给定时间内可以建立的连接数。您可以尝试处理输出中列出的所有异常,也可以尝试减少对您请求的 url 的尝试。
[编辑] 或者,如果您想暴力破解并处理所有错误和异常,请尝试以下操作
except:
j = j + 1
[编辑:]您可以尝试 https:
以及 http:
[编辑] 找到这个:
If the remote server is very slow, you can tell Requests to wait forever for a response, by passing None as a timeout value and then retrieving a cup of coffee.
r = requests.get('https://github.com', timeout=None)
关于Python 请求 : ignore exceptions and errors while connecting to proxy server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37419362/
根据我的理解,INSERT IGNORE 插入一个新条目,如果它不存在,如果存在,则忽略它。所以我已经尝试这样做了一段时间,但似乎没有用。这是我的尝试: insert insert ignore in
出于某种奇怪的原因,StyleCop 不尊重我的文档规则设置。考虑以下代码: internal class SomeClass { public SomeClass() { }
我有一个带有字符串参数和 bool 返回值的方法。我想确保它总是返回 true 我试过了 myClass.Foo(A.Ignored) .WithReturnType() .Returns(tru
这个问题在这里已经有了答案: Filter invoke twice when register as Spring bean (2 个答案) 关闭 1 年前。 我们正面临 SpringSecuri
将 iconv 与 //TRANSLIT//IGNORE 一起使用从 utf8 转换为 ascii 工作正常;它根据当前语言环境(在我的情况下为 de_DE)将不可转换的字符替换为正确的音译: > e
我有这样的查询: $query="INSERT IGNORE INTO mytable (key, word1, word2) VALUES (1, 'abc', 'def')"; 如果因为主键已经存
我有一个 MySQL 表,如下所示: `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `company_id` int(8) unsigned NOT N
git blame --ignore-revs-file显然是现代 Git 中存在的一个选项。 只有一个问题。它不起作用。 或者至少,它对我不起作用: 您可以将其添加到 shell 脚本中: mkdi
使用docker stack deploy,我可以看到如下信息: Ignoring unsupported options: restart 是否意味着重启政策没有到位? 是否必须在撰写文件之外指定它
我正在尝试检查被 git 忽略的文件,我发现 2 个命令显示相同的结果。 哪个是 git check-ignore * git ls-files --others --ignored --exclud
我想为我的 bash 设置和插件等创建一个 git 仓库。我忽略了所有内容(第 0 行),然后在 repo 中手动添加了我想要的文件/文件夹。 (我必须这样做,因为 repo 在我的 ~ 文件夹中。)
我们有一个集中式开发服务器,每个人都在本地结账处工作。我们如何仅忽略本地存储库中的特定目录,而不忽略集中存储库中的特定目录? 一些背景资料: 该项目是一个Drupal站点,该站点的文件目录一直在变化。
当我向 svn:ignore 条目添加一些东西时,它是存储在存储库中还是存储在我的本地副本中? (在 Tortoise 中,为什么添加到 svn:ignore 的项目会出现在我的更改列表中?) 最佳答
我想确保当我从 svn 结帐时,某些 ide 项目元数据文件没有更新。不幸的是,在创建项目时它已被 checkin 项目,但我有我自己的这些文件版本。 svn:ignore 是否也会在更新期间忽略文件
我有一个目录,其中包含具有各种扩展名的文件。我想在 svn:ignore 中执行与以下 .gitignore 代码等效的操作: *.* # ignore everything !.htaccess #
我在实时服务器上有一个大型项目,我想将其提交给 svn 存储库。我已使用 svn add 将所有文件和目录添加到存储库中. 问题是我想提交一个文件夹结构,但没有它的内容。文件夹名称是: /home/m
当我使用 svn:ignore 从 SVN 中排除文件时然后另一个人尝试提交排除的文件, svn 阻止他?或者他必须在他的电脑上执行相同的命令? 最佳答案 想象一下,如果您有一堆 Java 文件( *
如果已经有一个带有_id的文档,是否可以不重新索引文档?也许像MySQL中的INSERT IGNORE查询一样? 我使用批量API,并且重新索引需要很长时间,因此我只想索引丢失的文档。 可能吗? 最佳
我的房间有问题。 我正在使用带有 Gson 转换器的改造作为其余 api,我想与房间分享 pojos。一般来说它可以工作,但在某些情况下我需要忽略一些字段,因为我有对象列表。我尝试使用 @Ignore
我正在使用 vba 代码从数组中查找值。有些结果是“#N/A”,并且单元格的左上角会出现一个绿色三角形。我想自动删除绿色三角形(忽略错误),但“#N/A”结果应该保留,只需删除绿色三角形即可。 有人知
我是一名优秀的程序员,十分优秀!