相关疑难解决方法(0)

urllib2和asyncore之间的性能差异

我对这个简单的python脚本的性能有一些疑问:

import sys, urllib2, asyncore, socket, urlparse
from timeit import timeit

class HTTPClient(asyncore.dispatcher):
    def __init__(self, host, path):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect( (host, 80) )
        self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
        self.data = ''
    def handle_connect(self):
        pass
    def handle_close(self):
        self.close()
    def handle_read(self):
        self.data += self.recv(8192)
    def writable(self):
        return (len(self.buffer) > 0)
    def handle_write(self):
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]

url = 'http://pacnet.karbownicki.com/api/categories/'

components = urlparse.urlparse(url)
host = components.hostname or ''
path = components.path

def fn1():
    try:
        response = urllib2.urlopen(url)
        try: …
Run Code Online (Sandbox Code Playgroud)

python urllib2 ipv6 asyncore

12
推荐指数
1
解决办法
1287
查看次数

如何强制python httplib库只使用A请求

问题是使用httplib的urllib正在查询AAAA记录.

我想避免这种情况.有一个很好的方法吗?

>>> import socket
>>> socket.gethostbyname('www.python.org')
'82.94.164.162'


21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.python.org. (32)
21:52:37.312031 IP 192.168.0.1.53 > 192.168.0.9.44992: 27463 1/0/0 A 82.94.164.162 (48)


 python /usr/lib/python2.6/urllib.py -t http://www.python.org >/dev/null 2>&1

 21:53:44.118314 IP 192.168.0.9.40669 > 192.168.0.1.53: 32354+ A? www.python.org. (32)
21:53:44.118647 IP 192.168.0.9.40669 > 192.168.0.1.53: 50414+ AAAA? www.python.org. (32)
21:53:44.122547 IP 192.168.0.1.53 > 192.168.0.9.40669: 32354 1/0/0 A 82.94.164.162 (48)
21:53:44.135215 IP 192.168.0.1.53 > 192.168.0.9.40669: 50414 1/0/0 AAAA[|domain]
Run Code Online (Sandbox Code Playgroud)

python dns ipv4 ipv6

2
推荐指数
1
解决办法
2461
查看次数

标签 统计

ipv6 ×2

python ×2

asyncore ×1

dns ×1

ipv4 ×1

urllib2 ×1