本文整理汇总了Python中httplib.HTTPResponse.read方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.read方法的具体用法?Python HTTPResponse.read怎么用?Python HTTPResponse.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httplib.HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_http
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def process_http(raw):
headers, body = raw.split(BODY_SEP, 1)
if headers.startswith('CONNECT'):
return None
if not len(body):
return {
'headers': headers,
'body': None
}
source = FakeSocket(raw)
response = HTTPResponse(source)
response.begin()
body = None
if response.getheader('Content-Encoding') == 'gzip':
buf = StringIO(response.read(len(raw)))
f = gzip.GzipFile(fileobj=buf)
body = f.read()
else:
body = response.read(len(raw))
return {
'headers': headers,
'body': body if len(body) else None
}
示例2: setUp
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def setUp(self):
from httplib import HTTPResponse
with Stub() as HTTPResponse:
HTTPResponse.status >> 200
HTTPResponse.read() >> '{"id": "THE-PRECIOUS-GUID", "title": "example title", "subtitle": "example title - subtitle", "description": "a description", "user": "lfalvarez", "tags": [], "created_at": 1329767353.0, "source": "http://www.example.com/example.csv", "link": "http://www.junar.com/datastreams/some-url"}'
with Stub() as conn:
from httplib import HTTPConnection
conn = HTTPConnection("api.junar.com")
conn.request("POST", "/datastreams/publish", self.params, self.headers)
conn.getresponse() >> HTTPResponse
示例3: proxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def proxy(self):
if self.remote is None or self.lastHost != self.headers['Host']:
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.remote.connect((proxy_host, proxy_port))
self.remote.sendall(self.requestline.encode('ascii') + b'\r\n')
# Add auth inf.
self.headers['Proxy-Authorization'] = 'Basic %s' % base64.b64encode(auth)
#self.remote.sendall('Proxy-Authorization: Basic %s\r\n' % base64.b64encode(auth))
headerstr = str(self.headers).replace('\r\n', '\n').replace('\n', '\r\n')
self.remote.sendall(headerstr.encode('ascii') + b"\r\n")
# Send Post data
if self.command == 'POST':
self.remote.sendall(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.remote, method=self.command)
response.begin()
# Reply to the browser
status = 'HTTP/1.1 ' + str(response.status) + ' ' + response.reason
self.wfile.write(status.encode('ascii') + b'\r\n')
hlist = []
for line in response.msg.headers: # Fixed multiple values of a same name
if 'TRANSFER-ENCODING' not in line.upper():
hlist.append(line)
self.wfile.write(''.join(hlist) + b'\r\n')
if self.command == 'CONNECT' and response.status == 200:
return self.transfer(self.remote, self.connection)
else:
while True:
response_data = response.read(BufferSize)
if not response_data: break
self.wfile.write(response_data)
示例4: send_request
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def send_request(self, request):
self.request_num += 1
# Sanity check: if we're sending a request with a content-length but
# we don't have that many bytes to send, we'll just get a 504. Don't
# send it and instead report a client error.
parts = request.split('\r\n\r\n', 1)
if len(parts) > 1:
req, body = parts
match = content_length_re.search(req)
if match:
if len(body) < int(match.group(1)):
self.error("request body of incorrect size")
return True
if not self.connection:
self._connect()
if self.connection:
# tally request method
#self.tally(request.split(" ", 1)[0])
try:
self.connection.sendall(request)
response = HTTPResponse(self.connection)
response.begin()
self.tally(response.status)
while response.read():
pass
if response.will_close:
# We hope our Connection: keep-alive won't be ignored, but
# deal with it if it does.
self._disconnect()
if self.options.speedup < 0.8:
# if we're slowing down by too much, keep-alive will just
# result in the server getting bored between requests and
# dropping the connection, so disable it.
self._disconnect()
return True
except IncompleteRead:
self.error("error while reading response: IncompleteRead (terminating job)")
self._disconnect()
except Exception, e: # TODO: more restrictive error catching?
self.error("error while sending request and reading response: %s %s" % (type(e), e))
self._disconnect()
if self.connection:
self.connection.close()
self.connection = None
示例5: sogouProxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def sogouProxy(self):
if self.headers["Host"].startswith('chrome_dcp_proxy_pac.cnbeining'): #Give a PAC file
self.wfile.write("HTTP/1.1 200 OK".encode('ascii') + b'\r\n')
hstr = '''Host: 127.0.0.1
function FindProxyForURL(url, host) {
if (url.substring(0,5) == 'http:' &&
!isPlainHostName(host) &&
!shExpMatch(host, '*.local') &&
!isInNet(dnsResolve(host), '10.0.0.0', '255.0.0.0') &&
!isInNet(dnsResolve(host), '172.16.0.0', '255.240.0.0') &&
!isInNet(dnsResolve(host), '192.168.0.0', '255.255.0.0') &&
!isInNet(dnsResolve(host), '127.0.0.0', '255.255.255.0') )
return 'PROXY ''' + server_ip + ':' + str(server_port) + '''; DIRECT';
return 'DIRECT';
}'''
self.wfile.write(hstr + b'\r\n')
return
if self.remote is None or self.lastHost != self.headers["Host"]:
if PROXY_MODE == 'HTTPS':
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_default_certs()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(RemoteTimeout)
self.remote = context.wrap_socket(s, server_hostname='proxy.googlezip.net')
self.remote.connect(('proxy.googlezip.net', 443))
else: #HTTP
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.remote.settimeout(RemoteTimeout)
self.remote.connect(("compress.googlezip.net", 80))
self.remote.sendall(self.requestline.encode('ascii') + b"\r\n")
# Add Verification Tags
self.headers["Chrome-Proxy"] = get_google_header()
headerstr = str(self.headers).replace("\r\n", "\n").replace("\n", "\r\n")
self.remote.sendall(headerstr.encode('ascii') + b"\r\n")
# Send Post data
if self.command == 'POST':
self.remote.sendall(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.remote, method=self.command)
response.begin()
# Reply to the browser
status = "HTTP/1.1 " + str(response.status) + " " + response.reason
self.wfile.write(status.encode('ascii') + b'\r\n')
hlist = []
for line in response.msg.headers: # Fixed multiple values of a same name
if 'TRANSFER-ENCODING' not in line.upper():
hlist.append(line)
self.wfile.write("".join(hlist) + b'\r\n')
if self.command == "CONNECT": # NO HTTPS, as Chrome DCP does not allow HTTPS traffic
return
else:
while True:
response_data = response.read(BufferSize)
if not response_data: break
self.wfile.write(response_data)
示例6: do_proxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def do_proxy(self):
try:
if self.s == 0:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((proxy_host, proxy_port))
self.s.send(self.requestline.encode('ascii') + b'\r\n')
# Add Sogou Verification Tags
self.headers['X-Sogou-Auth'] = x_sogou_auth
t = hex(int(time.time()))[2:].rstrip('L').zfill(8)
self.headers['X-Sogou-Tag'] = calc_sogou_hash(t, self.headers['Host'])
self.headers['X-Sogou-Timestamp'] = t
self.s.send(str(self.headers).encode('ascii') + b'\r\n')
# Send Post data
if(self.command=='POST'):
self.s.send(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.s, method=self.command, buffering=True)
response.begin()
# Reply to the browser
status = 'HTTP/1.1 ' + str(response.status) + ' ' + response.reason
self.wfile.write(status.encode('ascii') + b'\r\n')
h = ''
for hh, vv in response.getheaders():
if hh.upper()!='TRANSFER-ENCODING':
h += hh + ': ' + vv + '\r\n'
self.wfile.write(h.encode('ascii') + b'\r\n')
while True:
response_data = response.read(8192)
if(len(response_data) == 0):
break
self.wfile.write(response_data)
except socket.error:
print('socket error for ' + self.requestline)
示例7: process_record
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def process_record(self, record):
content = None
try:
payload = record.payload.read()
s = FakeSocket(payload)
response = HTTPResponse(s)
response.begin()
status_code = response.status
if status_code != 200:
return
content_type = response.getheader('Content-Type', '')
if 'text/html' not in content_type:
return
headers = response.getheaders()
content = response.read(len(payload))
except Exception:
self.increment_counter('errors', 'process_record', 1)
logging.error('Error processing record: {}', traceback.format_exc())
return
if content is not None:
content = content.strip()
if not content:
return
for item in self.process_content(record.url, headers, content):
yield item
示例8: sogouProxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def sogouProxy(self):
if self.remote is None or self.lastHost != self.headers["Host"]:
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.remote.settimeout(RemoteTimeout)
self.remote.connect((proxy_host, proxy_port))
self.remote.sendall(self.requestline.encode('ascii').replace('baike.baidu.com', '220.181.111.247') + b"\r\n")
# Add Sogou Verification Tags
self.headers["X-Sogou-Auth"] = x_sogou_auth
t = hex(int(time.time()))[2:].rstrip('L').zfill(8)
self.headers["X-Sogou-Tag"] = calc_sogou_hash(t, self.headers['Host'])
self.headers["X-Sogou-Timestamp"] = t
headerstr = str(self.headers).replace("\r\n", "\n").replace("\n", "\r\n")
self.remote.sendall(headerstr.encode('ascii') + b"\r\n")
# Send Post data
if self.command == 'POST':
self.remote.sendall(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.remote, method=self.command)
response.begin()
# Reply to the browser
status = "HTTP/1.1 " + str(response.status) + " " + response.reason
self.wfile.write(status.encode('ascii') + b'\r\n')
hlist = []
for line in response.msg.headers: # Fixed multiple values of a same name
if 'TRANSFER-ENCODING' not in line.upper():
hlist.append(line)
self.wfile.write("".join(hlist) + b'\r\n')
if self.command == "CONNECT" and response.status == 200:
return self.transfer(self.remote, self.connection)
else:
while True:
response_data = response.read(BufferSize)
if not response_data: break
self.wfile.write(response_data)
示例9: send_and_recv
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def send_and_recv(self):
try:
# because www.dream-pro.info is tlanslated to 127.0.0.1 using hosts' entry,
# send message to www.dream-pro.info with socket.socket to make
# http connection
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((WWW_DREAM_PRO_INFO,80))
sock.sendall(str(self))
except:
print 'SocketError'
return
res = HTTPResponse(sock)
res.begin()
res_body = res.read()
res.close()
if 'transfer-encoding' in res.msg:
# httplib.HTTPResponse automatically concatenate chunked response
# but do not delete 'transfer-encoding' header
# so the header must be deleted
res.msg.__delitem__('transfer-encoding')
compmeth = res.msg.getheader('content-encoding','').lower()
if compmeth and compmeth.find('identity') != 0 :
# response body is compressed with some method
offset = 0
if compmeth.find('gzip') != -1:
# if body is gziped, header offset value is 47
# if not, offset value is 0
# this server does not support sdch...
offset += 47
res_body = decompress(res_body,offset)
res.msg['content-encoding'] = 'identity'
return res, res_body
示例10: get
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def get(self, request):
# Format and pass along the initial request
request = self.format_request(request)
self.outsocket.sendall(request)
# Wait for request
while(True):
r,w,x = select.select([self.insocket, self.outsocket],[],[],0)
if self.insocket in r:
request = self.insocket.recv(buff_size)
if request == "":
print "Got end message from browser"
self.kill()
else:
try:
request = self.format_request(request)
except MismatchedHost:
print "Host changed. Getting new end socket"
self.insocket.sendall("")
self.kill()
self.outsocket.sendall(request)
if self.outsocket in r:
httpRes = HTTPResponse(self.outsocket)
response = ""
try:
httpRes.begin()
headers = str(httpRes.msg)
content = httpRes.read()
if headers.find("Transfer-Encoding: chunked") != -1:
headers = headers.replace("Transfer-Encoding: chunked\r\n", "")
headers += "Content Length: " + str(len(content)) + "\r\n"
if httpRes.version == 10:
response += "HTTP/1.0 "
elif httpRes.version == 11:
response += "HTTP/1.1 "
response += str(httpRes.status) + " " + str(httpRes.reason) + nl
response += headers + nl
response += content
#print response
except BadStatusLine:
self.kill()
self.insocket.sendall(response)
self.kill()
#'''
if self.killed:
self.end()
return
示例11: get
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def get(addr, path, debug=False):
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(addr)
client.send("GET {} HTTP/1.0\r\n\r\n".format(path))
resp_str = client.recv(65536)
source = FakeSocket(resp_str)
resp = HTTPResponse(source)
resp.begin()
if resp.status == 200:
text = resp.read(len(resp_str))
data = json.loads(text)
if debug:
log.debug(data)
return data
return {}
示例12: proxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def proxy(self):
inWhileList = False
logging.info (self.requestline)
port = 80
host = self.headers["Host"]
if host.find(":") != -1:
port = int(host.split(":")[1])
host = host.split(":")[0]
(scm, netloc, path, params, query, _) = urlparse.urlparse(self.path)
if host in ["127.0.0.1", "localhost"]:
basedir = os.path.dirname(__file__)
htmlTemplate = os.path.join(basedir, "index.html")
htmlFile = open(htmlTemplate)
html = htmlFile.read()
htmlFile.close()
status = "HTTP/1.1 200 OK"
if path == "/save":
postData = self.rfile.read(int(self.headers['Content-Length']))
data = urlparse.parse_qs(postData)
logging.info(str(data))
key = data["id"][0]
value = data["value"][0]
if key in gConfig:
if type(gConfig[key]) == type(True):
if value == "true": gConfig[key] = True
if value == "false": gConfig[key] = False
else:
gConfig[key] = type(gConfig[key]) (value)
self.wfile.write(status + "\r\n\r\n" + value)
return
if path == "/add":
postData = self.rfile.read(int(self.headers['Content-Length']))
data = urlparse.parse_qs(postData)
if "BLOCKED_DOMAINS" in data:
domain = data["BLOCKED_DOMAINS"][0]
if domain[:4] == "http":
(scm, netloc, path, params, query, _) = urlparse.urlparse(domain)
domain = netloc
gConfig["BLOCKED_DOMAINS"][domain] = True
self.wfile.write("HTTP/1.1 302 FOUND\r\n" + "Location: /\r\n\r\n" + domain)
return
for key in gConfig:
if type(gConfig[key]) in [str,int] :
html = html.replace("{"+key+"}", str(gConfig[key]))
else :
html = html.replace("{" + key + "}", str(gConfig[key]))
self.wfile.write(status + "\r\n\r\n" + html)
return
try:
if (host in gConfig["HSTS_DOMAINS"]):
redirectUrl = "https://" + self.path[7:]
#redirect
status = "HTTP/1.1 302 Found"
self.wfile.write(status + "\r\n")
self.wfile.write("Location: " + redirectUrl + "\r\n\r\n")
return
for d in domainWhiteList:
if host.endswith(d):
logging.info (host + " in domainWhiteList: " + d)
inWhileList = True
connectHost = self.getip(host)
# Remove http://[host] , for google.com.hk
path = self.path[self.path.find(netloc) + len(netloc):]
if path == "":
path = "/"
if isDomainBlocked(host) or isIpBlocked(connectHost):
if gConfig['PROXY_TYPE'] == 'socks5':
self.remote = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
else:
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connectHost = gConfig['HTTP_PROXY']
port = gConfig['HTTP_PROXY_PORT']
path = self.path
else:
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.debug( host + ":connect to " + connectHost + ":" + str(port))
self.remote.connect((connectHost, port))
print " ".join((self.command, self.path, self.request_version)) + "\r\n"
self.remote.send(" ".join((self.command, path, self.request_version)) + "\r\n" + str(self.headers) + "\r\n")
# Send Post data
if(self.command=='POST'):
self.remote.send(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.remote, method=self.command)
badStatusLine = False
try :
response.begin()
print host + " response: %d"%(response.status)
except:
self.remote.close()
self.remote = None
#.........这里部分代码省略.........
示例13: HTTPResponse
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
req += self.rfile.read(int(self.headers['Content-Length']))
# Send it down the pipe!
self._proxy_sock.sendall(self.mitm_request(req))
# Parse response
h = HTTPResponse(self._proxy_sock)
h.begin()
# Get rid of the pesky header
del h.msg['Transfer-Encoding']
# Time to relay the message across
res = '%s %s %s\r\n' % (self.request_version, h.status, h.reason)
res += '%s\r\n' % h.msg
res += h.read()
# Let's close off the remote end
h.close()
self._proxy_sock.close()
# Relay the message
self.request.sendall(self.mitm_response(res))
def mitm_request(self, data):
for p in self.server._req_plugins:
data = p(self.server, self).do_request(data)
return data
def mitm_response(self, data):
for p in self.server._res_plugins:
示例14: HTTPResponse
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
# Append message body if present to the request
if 'Content-Length' in self.headers:
req += self.rfile.read(int(self.headers['Content-Length']))
# Send it down the pipe!
self._proxy_sock.sendall(self.mitm_request(req))
# Parse response
h = HTTPResponse(self._proxy_sock)
h.begin()
# Get rid of the pesky header
del h.msg['Transfer-Encoding']
content = active_content_log(h.read(), h.msg, self.headers['Host'])
# Time to relay the message across
res = '%s %s %s\r\n' % (self.request_version, h.status, h.reason)
res += '%s\r\n' % h.msg
res += content
# Let's close off the remote end
h.close()
self._proxy_sock.close()
# Relay the message
self.request.sendall(self.mitm_response(res))
def mitm_request(self, data):
for p in self.server._req_plugins:
示例15: proxy
# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import read [as 别名]
def proxy(self):
doInject = False
inWhileList = False
if gOptions.log > 0: print self.requestline
port = 80
host = self.headers["Host"]
if host.find(":") != -1:
port = int(host.split(":")[1])
host = host.split(":")[0]
try:
redirectUrl = self.path
while True:
(scm, netloc, path, params, query, _) = urlparse.urlparse(redirectUrl)
if gOptions.log > 2: print urlparse.urlparse(redirectUrl)
if (netloc not in gConfig["REDIRECT_DOMAINS"]):
break
prefixes = gConfig["REDIRECT_DOMAINS"][netloc].split('|')
found = False
for prefix in prefixes:
prefix = prefix + "="
for param in query.split('&') :
if param.find(prefix) == 0:
print "redirect to " + urllib.unquote(param[len(prefix):])
redirectUrl = urllib.unquote(param[len(prefix):])
found = True
continue
if not found:
break
if (host in gConfig["HSTS_DOMAINS"]):
redirectUrl = "https://" + self.path[7:]
#redirect
if (redirectUrl != self.path):
status = "HTTP/1.1 302 Found"
self.wfile.write(status + "\r\n")
self.wfile.write("Location: " + redirectUrl + "\r\n")
return
# Remove http://[host] , for google.com.hk
path = self.path[self.path.find(netloc) + len(netloc):]
connectHost = self.getip(host)
if (host in gConfig["BLOCKED_DOMAINS"]) or isIpBlocked(connectHost):
gConfig["BLOCKED_DOMAINS"][host] = True
if gOptions.log>0 : print "add ip "+ connectHost + " to block list"
return self.do_METHOD_Tunnel()
if True:
for d in domainWhiteList:
if host.endswith(d):
if gOptions.log > 0: print host + " in domainWhiteList: " + d
inWhileList = True
if not inWhileList:
doInject = self.enableInjection(host, connectHost)
self.remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if gOptions.log > 1: print "connect to " + host + ":" + str(port)
self.remote.connect((connectHost, port))
if doInject:
if gOptions.log > 0: print "inject http for "+host
self.remote.send("\r\n\r\n")
# Send requestline
if path == "":
path = "/"
print " ".join((self.command, path, self.request_version)) + "\r\n"
self.remote.send(" ".join((self.command, path, self.request_version)) + "\r\n")
# Send headers
if host[-12:] == ".appspot.com":
print "add version code " + gConfig["VERSION"] + " in HTTP header"
self.headers["X-WCProxy"] = gConfig["VERSION"]
self.headers["X-WCPasswd"] = gConfig["PROXY_PASSWD"]
self.remote.send(str(self.headers) + "\r\n")
# Send Post data
if(self.command=='POST'):
self.remote.send(self.rfile.read(int(self.headers['Content-Length'])))
response = HTTPResponse(self.remote, method=self.command)
badStatusLine = False
msg = "http405"
try :
response.begin()
print host + " response: %d"%(response.status)
msg = "http%d"%(response.status)
except BadStatusLine:
print host + " response: BadStatusLine"
msg = "badStatusLine"
badStatusLine = True
except:
raise
if doInject and (response.status == 400 or response.status == 405 or badStatusLine):
self.remote.close()
self.remote = None
if gOptions.log > 0: print host + " seem not support inject, " + msg
domainWhiteList.append(host)
return self.do_METHOD_Tunnel()
#.........这里部分代码省略.........