我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用gevent.socket.gethostbyname()。
def main(): urls = ['www.google.com', 'www.example.com', 'www.python.org'] jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls] gevent.joinall(jobs, timeout=2) print([job.value for job in jobs])
def patch_dns(): from gevent.socket import gethostbyname, getaddrinfo _socket = __import__('socket') _socket.getaddrinfo = getaddrinfo _socket.gethostbyname = gethostbyname
def parse_address(address): try: hostname, port = address.rsplit(':', 1) port = int(port) except ValueError: sys.exit('Expected HOST:PORT: %r' % address) return gethostbyname(hostname), port
def main(): with contextlib.closing(socket.socket()) as s, \ contextlib.closing(s.makefile('wb',0)) as writer, \ contextlib.closing(s.makefile('rb', 0)) as reader: # This will actually return a random testnet node their_ip = socket.gethostbyname("testnet-seed.bitcoin.schildbach.de") print("Connecting to:", their_ip) my_ip = "127.0.0.1" s.connect( (their_ip,PORT) ) stream = msg_stream(reader) # Send Version packet send(writer, version_pkt(my_ip, their_ip)) # Receive their Version their_ver = next(stream) print('Received:', their_ver) # Send Version acknolwedgement (Verack) send(writer, msg_verack()) # Fork off a handler, but keep a tee of the stream stream = tee_and_handle(writer, stream) # Get Verack their_verack = next(stream) # Send a ping! try: while True: send(writer, msg_ping()) send(writer, msg_getaddr()) gevent.sleep(5) except KeyboardInterrupt: pass
def gg(): import gevent from gevent import socket urls = ['www.baidu.com', 'www.qq.com', 'www.python.org'] jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls] gevent.joinall(jobs, timeout=2) print [job.value for job in jobs] # gg() # from urllib2 import urlopen # import gevent, time # # from gevent import monkey # ???????????IO?????????? # monkey.patch_all() # ??? # # # def f(url): # print 'GET:%s' % url # resp = urlopen(url) # data = resp.read() # # file = open("data",'wb')#?????????????? # # file.write(data) # print '%d bytes received from %s.' % (len(data), url) # # async_time_start = time.time() # gevent.joinall([ # gevent.spawn(f, 'https://www.python.org/'), # gevent.spawn(f, 'https://www.baidu.com/'), # gevent.spawn(f, 'https://github.com/') # ]) # print "???cost", time.time()-async_time_start # pyquery