本文整理汇总了Python中socket.setdefaulttimeout函数的典型用法代码示例。如果您正苦于以下问题:Python setdefaulttimeout函数的具体用法?Python setdefaulttimeout怎么用?Python setdefaulttimeout使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setdefaulttimeout函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testInetConnectivity
def testInetConnectivity(target="http://www.google.com"):
"""
test if we get an answer from the specified url
@param target:
@return: bool
"""
printl2("", "__common__::testInetConnectivity", "S")
import urllib2
from sys import version_info
import socket
try:
opener = urllib2.build_opener()
if version_info[1] >= 6:
page = opener.open(target, timeout=2)
else:
socket.setdefaulttimeout(2)
page = opener.open(target)
if page is not None:
printl2("success, returning TRUE", "__common__::testInetConnectivity", "D")
printl2("", "__common__::testInetConnectivity", "C")
return True
else:
printl2("failure, returning FALSE", "__common__::testInetConnectivity", "D")
printl2("", "__common__::testInetConnectivity", "C")
return False
except:
printl2("exception, returning FALSE", "__common__::testInetConnectivity", "D")
printl2("", "__common__::testInetConnectivity", "C")
return False
示例2: get_tags
def get_tags():
socket_to = None
try:
socket_to = socket.getdefaulttimeout()
socket.setdefaulttimeout(EC2.TIMEOUT)
except Exception:
pass
try:
iam_role = urllib2.urlopen(EC2.URL + "/iam/security-credentials").read().strip()
iam_params = json.loads(urllib2.urlopen(EC2.URL + "/iam/security-credentials" + "/" + unicode(iam_role)).read().strip())
from checks.libs.boto.ec2.connection import EC2Connection
connection = EC2Connection(aws_access_key_id=iam_params['AccessKeyId'], aws_secret_access_key=iam_params['SecretAccessKey'], security_token=iam_params['Token'])
instance_object = connection.get_only_instances([EC2.metadata['instance-id']])[0]
EC2_tags = [u"%s:%s" % (tag_key, tag_value) for tag_key, tag_value in instance_object.tags.iteritems()]
except Exception:
log.exception("Problem retrieving custom EC2 tags")
EC2_tags = []
try:
if socket_to is None:
socket_to = 3
socket.setdefaulttimeout(socket_to)
except Exception:
pass
return EC2_tags
示例3: start_scheduler
def start_scheduler(self):
c = self.colored
if self.pidfile:
platforms.create_pidlock(self.pidfile)
beat = self.Service(app=self.app,
max_interval=self.max_interval,
scheduler_cls=self.scheduler_cls,
schedule_filename=self.schedule)
print(str(c.blue('__ ', c.magenta('-'),
c.blue(' ... __ '), c.magenta('-'),
c.blue(' _\n'),
c.reset(self.startup_info(beat)))))
self.setup_logging()
if self.socket_timeout:
logger.debug('Setting default socket timeout to %r',
self.socket_timeout)
socket.setdefaulttimeout(self.socket_timeout)
try:
self.install_sync_handler(beat)
beat.start()
except Exception, exc:
logger.critical('celerybeat raised exception %s: %r',
exc.__class__, exc,
exc_info=True)
示例4: discover
def discover(service, timeout=2, retries=1):
group = ("239.255.255.250", 1900)
message = "\r\n".join([
'M-SEARCH * HTTP/1.1',
'HOST: {0}:{1}',
'MAN: "ssdp:discover"',
'ST: {st}','MX: 3','',''])
socket.setdefaulttimeout(timeout)
responses = {}
for _ in range(retries):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto(message.format(*group, st=service), group)
while True:
try:
response = SSDPResponse(sock.recv(1024))
responses[response.location] = response
except socket.timeout:
break
return responses.values()
# Example:
# import ssdp
# ssdp.discover("roku:ecp")
示例5: handle_request
def handle_request( self, trans, url, http_method=None, **kwd ):
if 'Name' in kwd and not kwd[ 'Name' ]:
# Hack: specially handle parameters named "Name" if no param_value is given
# by providing a date / time string - guarantees uniqueness, if required.
kwd[ 'Name' ] = time.strftime( "%a, %d %b %Y %H:%M:%S", time.gmtime() )
if 'Comments' in kwd and not kwd[ 'Comments' ]:
# Hack: specially handle parameters named "Comments" if no param_value is given
# by providing a date / time string.
kwd[ 'Comments' ] = time.strftime( "%a, %d %b %Y %H:%M:%S", time.gmtime() )
socket.setdefaulttimeout( 600 )
# The following calls to urllib2.urlopen() will use the above default timeout.
try:
if not http_method or http_method == 'get':
page = urllib2.urlopen( url )
response = page.read()
page.close()
return response
elif http_method == 'post':
page = urllib2.urlopen( url, urllib.urlencode( kwd ) )
response = page.read()
page.close()
return response
elif http_method == 'put':
url += '/' + str( kwd.pop( 'id' ) ) + '?key=' + kwd.pop( 'key' )
output = self.put( url, **kwd )
except Exception, e:
raise
message = 'Problem sending request to the web application: %s. URL: %s. kwd: %s. Http method: %s' % \
( str( e ), str( url ), str( kwd ), str( http_method ) )
return self.handle_failure( trans, url, message )
示例6: myproxy
def myproxy(url):
req = urllib2.Request(url)
try:
# Important or if the remote server is slow
# all our web server threads get stuck here
# But this is UGLY as Python does not provide per-thread
# or per-socket timeouts thru urllib
orignal_timeout = socket.getdefaulttimeout()
try:
socket.setdefaulttimeout(60)
response = urllib2.urlopen(req)
finally:
# restore orignal timeoout
socket.setdefaulttimeout(orignal_timeout)
# XXX: How to stream respone through Zope
# AFAIK - we cannot do it currently
return response.read()
except HTTPError, e:
# Have something more useful to log output as plain urllib exception
# using Python logging interface
# http://docs.python.org/library/logging.html
logger.error("Server did not return HTTP 200 when calling remote proxy URL:" + url)
for key, value in params.items():
logger.error(key + ": " + value)
# Print the server-side stack trace / error page
logger.error(e.read())
raise e
示例7: __init__
def __init__(self, host, port, locationTemplate, timeout=5, ssl=False):
"""Create an instance of *HttpReader* bound to specific URL.
Note:
The `http_proxy` and `https_proxy` environment variables are
respected by the underlying `urllib` stdlib module.
Args:
host (str): domain name or IP address of web server
port (int): TCP port web server is listening
locationTemplate (str): location part of the URL optionally containing @[email protected]
magic placeholder to be replaced with MIB name. If @[email protected] magic is not present,
MIB name is appended to `locationTemplate`
Keyword Args:
timeout (int): response timeout
ssl (bool): access HTTPS web site
"""
self._url = '%s://%s:%d%s' % (ssl and 'https' or 'http',
host, port, decode(locationTemplate))
socket.setdefaulttimeout(timeout)
self._user_agent = 'pysmi-%s; python-%s.%s.%s; %s' % (
pysmi_version, sys.version_info[0], sys.version_info[1],
sys.version_info[2], sys.platform
)
示例8: changep
def changep(self):
print 'changing proxy'
socket.setdefaulttimeout(3.0)
test_url = 'http://www.baidu.com'
while True:
try:
proxy = random.choice(Proxypool)
proxy = 'http://'+proxy
print proxy
except:
continue
try:
start = time.time()
f = urllib.urlopen(test_url,proxies={'http':proxy})
f.close()
except:
continue
else:
end=time.time()
dur = end - start
print proxy,dur
if dur <= 2:
print 'proxy changed to'
print proxy
self.proxy = proxy
self.proxy_usetime =0
break
else:
continue
示例9: downloadPackage
def downloadPackage(strUrl, dest, module, timeout=120):
print strUrl, dest, region, module
print "--- prepare to download data for " + module
if not os.path.exists(dest):
os.makedirs(dest)
socket.setdefaulttimeout(timeout)
component = strUrl.split("/")[-1]
componentTar = os.path.splitext(component)[-2]
try:
urllib.urlretrieve(strUrl, dest + os.sep + component)
except socket.error:
errno, errstr = sys.exc_info()[:2]
if errno == socket.timeout:
print "There was a timeout"
print "--- Download failed ---"
print "--- Clean the broken data ---"
os.remove(dest + os.sep + component)
print "--- Start to retry downloading ---"
urllib.urlretrieve(strUrl, dest + os.sep + component)
else:
print "There was other socket error!"
except Exception, e:
print "Other exceptions!", e
print "--- Download failed ---"
print "--- Clean the broken data ---"
os.remove(dest + os.sep + component)
print "--- Start to retry downloading ---"
urllib.urlretrieve(strUrl, dest + os.sep + component)
示例10: da_connect
def da_connect(ip, port):
try:
socket.setdefaulttimeout(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
s.send('Hello! This is EVIILLLE calling! Anyone home?')
data = s.recv(1024)
print 'We hit something! Received' + str(data)
print 'Logging hit on the network.'
fo = open("hits.txt", "a")
now = time.strftime("%c")
# date and time representation
print "Current date & time " + time.strftime("%c")
toFoOut = "We hit " + str(ip) + ": " + str(port) + ": " + time.strftime("%c")
# write to file
fo.write(toFoOut + '\n')
fo.close()
print 'Wrote to log file.'
# We want to close the connection after we opened it.
# This is for logging open ports and nothing more at the moment.
# Muhahahahahahaha.
s.close()
s.shutdown()
except:
return
示例11: main
def main(args, loglevel):
logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel)
socket.setdefaulttimeout(args.timeout)
g = Github()
with open(args.repo_file, 'r') as f:
file_counter = 0
for line in f.readlines():
logging.info('Fetching repository: %s' % line)
try:
repo_str = line.rstrip().split('github.com/')[-1]
repo = g.get_repo(repo_str)
tree = repo.get_git_tree('master', recursive=True)
files_to_download = []
for file in tree.tree:
if fnmatch.fnmatch(file.path, args.wildcard):
files_to_download.append('https://github.com/%s/raw/master/%s' % (repo_str, file.path))
for file in files_to_download:
logging.info('Downloading %s' % file)
file_counter += 1
filename = posixpath.basename(urlparse.urlsplit(file).path)
output_path = os.path.join(args.output_dir, filename)
if os.path.exists(output_path):
output_path += "-" + str(file_counter)
try:
urllib.urlretrieve(file, output_path)
except:
logging.error('Error downloading %s' % file)
except:
logging.error('Error fetching repository %s' % line)
示例12: scan_block
def scan_block(self):
""" ARPing the local network
"""
conf.verb = 0
print '[!] Beginning host scan with netmask %s...' % (self.net_mask)
try:
start = datetime.now()
(ans, unans) = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=self.net_mask),timeout=1, inter=0.1,multi=True)
elapsed = (datetime.now() - start).seconds
print '[!] Scan of %s completed in %s seconds with %d hosts responding.'%(self.net_mask,elapsed,len(ans))
for s, r in ans:
ip = r[ARP].getfieldval('psrc')
mac = r[ARP].getfieldval('hwsrc')
if self.fingerprint:
host = ''
try:
if hasattr(socket, 'setdefaulttimeout'):
socket.setdefaulttimeout(3)
host = socket.gethostbyaddr(ip)[0]
except:
host = ''
print "\t%s : %s (%s)" % (mac, ip, host)
self._dbhost(mac, ip, host)
else:
print '\t%s : %s' % (mac, ip)
self._dbhost(mac, ip, '')
self.available_hosts[mac] = ip
except Exception:
Error('Error with net mask. Cannot scan given block.')
return
print '\n'
示例13: setup_modules
def setup_modules():
"""
Perform any global module setup needed by MyPyTutor.
"""
import socket
socket.setdefaulttimeout(GLOBAL_TIMEOUT)
示例14: _start_server
def _start_server(self):
"""Start up a viewing server."""
# Start viewer server
# IMPORTANT: if running in an IPython/Jupyter notebook,
# use the no_ioloop=True option
from ginga.web.pgw import ipg
if not self._port:
import socket
import errno
socket.setdefaulttimeout(0.05)
ports = [p for p in range(8800, 9000) if
socket.socket().connect_ex(('127.0.0.1', p)) not in
(errno.EAGAIN, errno.EWOULDBLOCK)]
self._port = ports[0]
self._server = ipg.make_server(host=self._host,
port=self._port,
use_opencv=self.use_opencv,
numthreads=self._threads)
try:
backend_check = get_ipython().config
except NameError:
backend_check = []
no_ioloop = False # ipython terminal
if 'IPKernelApp' in backend_check:
no_ioloop = True # jupyter console and notebook
self._server.start(no_ioloop=no_ioloop)
示例15: discover
def discover(timeout=1.5):
"""Crude SSDP discovery. Returns a list of RxvDetails objects
with data about Yamaha Receivers in local network"""
socket.setdefaulttimeout(timeout)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto(SSDP_MSEARCH_QUERY.encode("utf-8"), (SSDP_ADDR, SSDP_PORT))
responses = []
try:
while True:
responses.append(sock.recv(10240))
except socket.timeout:
pass
results = []
for res in responses:
m = re.search(r"LOCATION:(.+)", res.decode('utf-8'), re.IGNORECASE)
if not m:
continue
url = m.group(1).strip()
res = rxv_details(url)
if res:
results.append(res)
return results