Comments (4)
You can use asyncio instead of select.poll. In python 2.7, asyncio is named trollius .
https://github.com/saghul/pycares/blob/master/examples/cares-asyncio.py
from pycares.
I'm stuck on python2.7 and i also have to use gevent's monkey patch.
from pycares.
adapted from pycares's pyuv and select example. The following example is also worked well for select.epoll, just replace
self.poll = select.poll()
by
self.poll = select.epoll()
code:
import pycares
import select
import socket
class DNSResolver(object):
def __init__(self):
self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb, servers=['1.1.1.1'], timeout=5.0, flags=pycares.ARES_FLAG_USEVC)
#self._channel = pycares.Channel(sock_state_cb=self._sock_state_cb)
self.poll = select.poll()
self._fd_map = set()
def _sock_state_cb(self, fd, readable, writable):
print "fd {} read {} write {}".format(fd, readable, writable)
if readable or writable:
event = (select.POLLIN if readable else 0) | (select.POLLOUT if writable else 0)
if fd not in self._fd_map:
# New socket
print "register %d" % fd
self.poll.register(fd, event)
self._fd_map.add(fd)
else:
print "modify %d" % fd
self.poll.modify(fd, event)
else:
# Socket is now closed
self._fd_map.remove(fd)
print "unregister %d" % fd
self.poll.unregister(fd)
def wait_channel(self):
while True:
if not self._fd_map:
break
timeout = self._channel.timeout(1.0)
if not timeout:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
continue
for fd, event in self.poll.poll(timeout):
if event & (select.POLLIN | select.POLLPRI):
self._channel.process_fd(fd, pycares.ARES_SOCKET_BAD)
if event & select.POLLOUT:
self._channel.process_fd(pycares.ARES_SOCKET_BAD, fd)
def query(self, query_type, name, cb):
self._channel.query(query_type, name, cb)
def gethostbyname(self, name, cb):
self._channel.gethostbyname(name, socket.AF_INET, cb)
if __name__ == '__main__':
def query_cb(result, error):
print result
print error
def gethostbyname_cb(result, error):
print result
print error
resolver = DNSResolver()
resolver.query('google.com', pycares.QUERY_TYPE_A, query_cb)
resolver.query('facebook.com', pycares.QUERY_TYPE_A, query_cb)
resolver.query('sip2sip.info', pycares.QUERY_TYPE_SOA, query_cb)
resolver.gethostbyname('apple.com', gethostbyname_cb)
resolver.wait_channel()
from pycares.
Thanks for chiming in @boytm ! Closing this since an example was provided.
from pycares.
Related Issues (20)
- gethostbyname() does not appear to work with lookups="f"
- python3 setup.py build failed in [email protected] on centos8_aarch64 HOT 2
- CNAME answers to A request HOT 6
- pytest failed in [email protected] on centos8_aarch64 HOT 1
- Error FileNotFound when installing pycares==4.1.1 HOT 8
- 4.1.2: pytest is failing HOT 5
- `pycares.reverse_address` Import Error HOT 2
- Security vulnerability HOT 2
- 4.2.0: sphinx warnings `reference target not found` HOT 2
- 3.11 wheels HOT 2
- Can't build from sources on EL8 HOT 3
- 4.2.2: pytest is failing in two units HOT 8
- Fail to install on centos docker HOT 4
- 4.3.0: pytest is failing in `tests/test_all.py::DNSTest::test_custom_resolvconf` unit HOT 5
- test_query_class_chaos started failing
- TLSA record HOT 1
- pycares fails to install in arm8 android device HOT 16
- Update c-ares to 1.24 HOT 1
- [NOT_CRITICAL] Missing Flags: ARES_FLAG_EDNS, ARES_FLAG_NO_DFLT_SVR HOT 1
- Intermittent freeze: Channel.query() call never returning HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pycares.