Listening and capturing multiple response for a dns query in python - python

I am trying to send a dns query to an specific dns resolver in python.
Here is my code to set my resolver and sending my dns query to find the ip address of my domain name:
import dns.resolver #import the module
myResolver = dns.resolver.Resolver()
myResolver.nameservers = ['my resolver ip address']
myResolver.port = 5300#port number of resolver listening to dns queries
myAnswers = myResolver.query("google.com")
print myAnswers
This code works very well when I receive just one response from the resolver. The response can has multiple ip addresses and that is ok too. The question is that, my dns resolver has been configured to send multiple response to a A dns query and I need to receive first, second and third one and check them and if one of them passed my checks, print the response in the console.
My problem is that I do not know how can I get multiple responses with myAnswers = myResolver.query("google.com") command. Because this command return the response and save that in myAnswer. I know I should use multithreading and listen to incoming responses from my dns resolver. But I do not know how I can run my thread with this command,myAnswers = myResolver.query("mytargetdomain.com",'a')?
Thanks.

Related

Lamdba aws to access direct IP with the domain in host header

I have a small python code which works fine localy but when I run it on AWS it doesn't work.
Instead of calling a domain I'm directing the traffic to an IP and passing the domain under "Host" in headers.
I have an external application which running under 2 different IPs:
Example:
1.1.1.1
and
2.2.2.2
I want to send request to #2 (2.2.2.2).
Lets say that both running the application which configured under my.app.com.
I want to make a request "my.app.com" so it'll route to 2.2.2.2.
Even though the public A record is set to 1.1.1.1.
So when I run this in Python:
import urllib.request
headers = {"Host":"my.app.com"}
url = 'http://1.1.1.1'
req = urllib.request.Request(url=url,headers=headers)
res = urllib.request.urlopen(req)
response = res.read()
It works OK from my lock computer but when trying to run it from AWS it fails , eventually the lambda is timing out.
If you configured lambda inside VPC, please make sure it sits in the private subnet and also NAT gateway to access external traffic)

DNS Verifying email records via Python

First off.. excuse my complete noobness when it comes to DNS protocols.
Im trying to use python to verify if email exists (using this code as base)
import re
import smtplib
import dns.resolver
# Address used for SMTP MAIL FROM command
fromAddress = 'corn#bt.com'
# Simple Regex for syntax checking
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
# Email address to verify
inputAddress = input('Please enter the emailAddress to verify:')
addressToVerify = str(inputAddress)
# Syntax check
match = re.match(regex, addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
# Get domain for DNS lookup
splitAddress = addressToVerify.split('#')
domain = str(splitAddress[1])
print('Domain:', domain)
# MX record lookup
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()
#print(code)
#print(message)
# Assume SMTP response 250 is success
if code == 250:
print('Success')
else:
print('Bad')
This works fine on a Digitalocean VPS server
But this code doesnt work on local machine (timeout error). So I thought it's a port issue, however port 25 isn't open on my server.
So I took the string value of server.local_hostname (which , by the way.. isn't my domain.. AND i haven't config'd an STMP .. AND as I mentioned before, I haven't opened up port 25) and run it on my local machine, i still get time out error.
1) There's something I must not be understanding when it comes to DNS protocol.. any ideas?
2) If there's some DNS protocol happening between SMTP servers, is it possible to use a proxy server to validate email?
It is impossible to verify that an e-mail address exists.
Plenty of servers will accept mail to any address and discard from there. Many addresses are aliases for others. Certainly, there isn't some magic DNS record that tells you whether or not a particular e-mail address exists. That would be simply inviting spam from people who scrape DNS records to spam people.

How to get the ip address of a request machine using python (On Sanic)

I am trying to get the IP address of the requesting computer on my server. I can successfully get the IP-address from request header if the request came from a web browser. The code example below. However, I cannot fetch the client IP, if I send the request via curl/postman. I checked the nginx log, and I found there is a log of my public IP when I sent a curl request. How can I achieve this?
PS: I am using the Sanic Framework.
client_ip = request.headers.get('x-real-ip')
In sanic's docs for Request Data:
ip (str) - IP address of the requester.
Therefore, just use
client_ip = request.ip

Python HTTP HEAD request keepalive

Using Python httplib or httpclient, what code do I need to use in my HTTP client to:
use an HTTP HEAD request and
contact a web server by just specifying only its IP address and
contact a web server without specifying any webpage (or homepage) on the request
to extend its HTTP connection using Keepalive messages?
I used the following code example but it has two problems:
It does not extend the http connection using Keepalive,
It gives me an error message "500 Domain Not Found" if I use the IP address instead of the domain name.
import http.client
Connection = http.client.HTTPConnection("www.python.org")
Connection.request("HEAD", "")
response = Connection.getresponse()
print(response.status, response.reason)
requests allows to:
send requests with HEAD method:
import requests
resp = requests.head("http://www.python.org")
use sessions for auto Keep-alive: info
s = requests.Session()
resp = s.head("http://www.python.org")
resp2 = s.get("http://www.python.org/")
Regarding using the IP address instead of domain, that has nothing to do with your request. Most sites use some kind of virtual hosts, so they don't respond to IP address only to specific domain names. If you ask for the IP address you may get a 500 error or a message error.

about get client ip address in python CGI

I am trying to get the original client ip address in a python cgi script. The client connect to the web server with a proxy. Following code always returns the proxy ip address. I tested all the env variable, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR all return None. Is there any other way to get the client ip behind a proxy? like can I read the http header in a python cgi?
ipaddr = (getenv("HTTP_CLIENT_IP") or
getenv("HTTP_X_FORWARDED_FOR") or
getenv("REMOTE_ADDR") or
"UNKNOWN")
Do you want to:
find the client's IP address in the headers
use some sort of javascript to tell you?
For the former, you need the proxy to give you the client address.
Have you tried cgi.print_environ_usage() and looking for the IP address?

Categories

Resources