I want to resolve an IP to a hostname from a specific DNS server.
socket.gethostbyaddr() uses default DNS server. I need to resolve ip with specific DNS server.
I saw dnspython but do not know how to specify the DNS server to use for reverse lookup.
Try this:
import dns.resolver
dns.resolver.default_resolver = dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers = ['8.8.8.8']
answers = dns.resolver.query(<addr>, 'PTR')
for rdata in answers:
print(rdata)
Related
Is it possible to get the IP of the default interface with Sanic?
Here is how I do it with Socket. The idea is to do the same thing with Sanic.
import socket
hostname = socket.gethostname()
IP_address = socket.gethostbyname(hostname)
print(IP_address) # 192.168.1.239
It depends upon what information you want and how the app is being served (reverse proxy, etc).
Check out these values:
request.ip (connected interface)
request.remote_addr (likely what you want https://sanic.readthedocs.io/en/stable/sanic/api/core.html#sanic.request.Request.remote_addr)
request.conn_info (object with a bunch of details you may want)
I want to get the client's computername and computer login user in my django project in the intranet. So I use wmi with the ip to get that infomation. However some ip could not be connected by wmi, and come up with an error called "The RPC Server is unavailable". Then I try to use the computername to connect by wmi for testing, it worked. What causes this problem? I have used socket.getnamebyaddr also get the wrong computername.
'''
import wmi
ip = request.META.get('REMOTE_ADDR')
try:
conn = wmi.WMI(computer = ip,user = 'xx', password="xx")
for each in conn.Win32_ComputerSystem():
content = {
'user':json.dumps(each.UserName),
'comname':json.dumps(each.Name),
}
print(each.Name)
print(each.UserName)
'''
Use socket module instead of wmi.
import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
More information can be found here
I have a django application using this function, i'm trying to get the computer name of the ip address accessing my application. I'm doing this by using django-ipware to get the client's ip address, this part is working fine. Then i'm using socket.gethostbyaddr() to get the client's computer name, this works fine on my windows development machine.
def get_comp_name(request):
client_ip = get_client_ip(request)
try:
comp_name = socket.gethostbyaddr(client_ip[0])[0]
except socket.herror:
comp_name = ''
When i tried to deploy to a centOS 7 machine, I get the following error when executing socket.gethostbyaddr() on local network ip addresses.
socket.herror: [Errno 1] Unknown host
I can ping local ip addresses without issue. Am I missing a configuration on my centOS 7 machine?
Your DNS server needs to have an entry for this to work. Check /etc/resolv.conf if the DNS server IP is correct, check if DNS server is reachable from the CentOS node and finally, check if the entry in the DNS server is correct.
I use dsnPython in a project.
I use many resolvers same as explained at Set specific DNS server using dns.resolver (pythondns).
In order to send several requests I need to dispatch my request on many IPs.
I have some IPs on my interface eth0.
Do you know a way to send a request through an specific IP ?
It's possible by using resolvers and source attribute :
import dns.resolver
my_resolver.nameservers = ['8.8.8.8']
answer = my_resolver.query(
qname = fqdn_port,
source = '1.2.3.4',
)
8.8.8.8 is the resolver IP
1.2.3.4 is an IP of server
I need client IP address using python. I have tried below code but its not working in server:
from socket import gethostname, gethostbyname
ip = gethostbyname(gethostname())
print ip
On the server, I get '127.0.0.1' every time. Is there any way to find IP address of the client?
You're getting the IP address of your server, not of your server's clients.
You want to look at the request's REMOTE_ADDR, like this:
from bottle import Bottle, request
app = Bottle()
#app.route('/hello')
def hello():
client_ip = request.environ.get('REMOTE_ADDR')
return ['Your IP is: {}\n'.format(client_ip)]
app.run(host='0.0.0.0', port=8080)
EDIT #1: Some folks have observed that, for them, the value of REMOTE_ADDR is always the same IP address (usually 127.0.0.1). This is because they're behind a proxy (or load balancer). In this case, the client's original IP address is typically stored in header HTTP_X_FORWARDED_FOR. The following code will work in either case:
#app.route('/hello')
def hello():
client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
return ['Your IP is: {}\n'.format(client_ip)]
EDIT #2: Thanks to #ArtOfWarfare's comment, I learned that REMOTE_ADDR is not required per PEP-333. Couple of observations:
The CGI spec does require REMOTE_ADDR:
The REMOTE_ADDR variable MUST be set to the network address of the client sending the request to the server.
However, PEP-333 does not explicitly require HTTP_REMOTE_ADDR, only going as far as this (emphasis mine):
A server or gateway SHOULD attempt to provide as many other CGI variables as are applicable.
All of the (admittedly few) web frameworks that I'm familiar with set HTTP_REMOTE_ADDR. AFAICT, it's a de facto "standard." But technically, YMMV.
Server might be behind a proxy. Use this for proxy and forward support:
request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
If you're trying to get the external IP, you will need to get it from an external source, i.e whatismyip.com or somewhere that offers an api. If that's what you're looking for, take a look at the Requests module http://docs.python-requests.org/