这只需要在单个子网上工作,不得恶意使用.
我有一个用Python编写的负载测试工具,它基本上会在URL上发出HTTP请求.我需要针对基于IP的负载均衡器运行性能测试,因此请求必须来自一系列IP.大多数商业性能工具都提供此功能,但我想将其构建到我自己的工具中.
该工具使用Python的urllib2进行传输.是否可以为构成请求的数据包发送带有欺骗IP地址的HTTP请求?
我的目的是在输入谷歌搜索词后从第一页的所有链接中提取html.我在代理后面工作,所以这是我的方法.
1.我首先使用mechanize在表单中输入搜索词,我已正确设置代理和机器人.
2.提取链接后,我使用了全局使用urllib2.ProxyHandler的开启工具,单独打开网址.
但是这给了我这个错误.无法搞清楚.
urlopen error [Errno 8] _ssl.c:504: EOF occurred in violation of protocol
Run Code Online (Sandbox Code Playgroud) 我正在使用python的urllib2库向特定主机发出几个http请求.每次发出请求时,都会创建一个新的tcp和http连接,这需要花费大量时间.有没有办法使用urllib2保持tcp/http连接活着?
我用urllib2的是build_opener()创造一个OpenerDirector.我正在使用它OpenerDirector来获取慢速页面,因此它有一个很大的超时.
到现在为止还挺好.
但是,在另一个线程中,我被告知要中止下载 - 假设用户已经选择退出GUI中的程序.
有没有办法发出urllib2下载应该退出的信号?
我正在尝试实现PayPal IPN功能.基本协议如下:
当我尝试urllib.urlencode PayPal发送给我的params时,我得到一个:
While calling send_response_to_paypal. Traceback (most recent call last):
File "<snip>/account/paypal/views.py", line 108, in process_paypal_ipn
verify_result = send_response_to_paypal(params)
File "<snip>/account/paypal/views.py", line 41, in send_response_to_paypal
params = urllib.urlencode(params)
File "/usr/local/lib/python2.6/urllib.py", line 1261, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我知道urlencode执行ASCII编码,在某些情况下,用户的联系信息可以包含非ASCII字符.这是可以理解的.我的问题是,如何使用urllib2.urlopen(req)(或其他方法)将非ASCII字符编码为POST到URL
细节:
我在PayPal的原始请求中读取了以下内容(GET用于测试):
def read_ipn_params(request):
if request.POST:
params= request.POST.copy()
if "ipn_auth" in request.GET:
params["ipn_auth"]=request.GET["ipn_auth"]
return params
else:
return request.GET.copy()
Run Code Online (Sandbox Code Playgroud)
我用来从处理页面向PayPal发回请求的代码是:
def send_response_to_paypal(params):
params['cmd']='_notify-validate'
params …Run Code Online (Sandbox Code Playgroud) 我正在使用该urllib2.urlopen方法打开URL并获取网页的标记.其中一些网站使用301/302重定向重定向我.我想知道我被重定向到的最终URL.我怎么能得到这个?
>> url = 'https://test.authorize.net/gateway/transact.dll'
>> data = {'x_login': 'abc123', 'x_type': 'AUTH_CAPTURE', 'x_card_num': '4444333322221103', 'x_amount': '50.75', 'x_tran_key
': 'abc123', 'x_version': '3.1', 'x_delim_char': '|', 'x_exp_date': '022012', 'x_delim_data': 'TRUE'}
>>
>> urllib2.urlopen(url, data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gateways\base.py", line 81, in dispatch
return gw_method(self, *args, **kwargs)
File "gateways\decorators.py", line 17, in wrapper
method(*args, **kwargs)
File "gateways\authorize_net.py", line 39, in auth_capture
return self.post_data(data)
File "gateways\authorize_net.py", line 43, in post_data
raw_response = urllib2.urlopen(self.get_endpoint(), data)
File "C:\Python26\lib\urllib2.py", line …Run Code Online (Sandbox Code Playgroud) 我正在寻找有关urllib2和httplib的线程安全性的信息.官方文档(http://docs.python.org/library/urllib2.html和http://docs.python.org/library/httplib.html)缺少有关此主题的任何信息; 那里没有提到线程这个词......
UPDATE
好吧,它们不是开箱即用的线程安全.是什么使它们成为线程安全的,或者是否存在可以线程安全的场景?我问,因为它似乎是
urllib2在每个线程中使用单独的足以在线程中安全地使用这些库.类似的使用场景提出了问题urllib2和cookielib线程安全性
我使用以下代码来使用Python保存网页:
import urllib
import sys
from bs4 import BeautifulSoup
url = 'http://www.vodafone.de/privat/tarife/red-smartphone-tarife.html'
f = urllib.urlretrieve(url,'test.html')
Run Code Online (Sandbox Code Playgroud)
问题:此代码将html保存为基本的html,没有javascripts,图像等.我想将网页保存为完整(就像我们在浏览器中有选项一样)
更新:我现在使用以下代码来保存webapge的所有js/images/css文件,以便它可以保存为完整的网页,但我的输出html仍然像基本的html一样保存:
import pycurl
import StringIO
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://www.vodafone.de/privat/tarife/red-smartphone-tarife.html")
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.perform()
html = b.getvalue()
#print html
fh = open("file.html", "w")
fh.write(html)
fh.close()
Run Code Online (Sandbox Code Playgroud) 我需要在服务器和远程Web服务之间创建一个安全通道.我将使用带有客户端证书的HTTPS.我还需要验证远程服务提供的证书.
如何在urllib2中使用自己的客户端证书?
我需要在代码中做些什么才能确保远程证书正确无误?