I am fairly new to Python (just started today) but am really struggling with this:
#! /usr/bin/python
import socket
s=socket.socket()
s.connect(("<my ip addres>", 22))
answer=s.recv(1024)
print(answer)
s.close
I believe I am meant to be getting some sort of 'banner' response but it gives nothing for a few minutes before spouting the following error:
TimeoutError: [Errno 60] Operation timed out
Why would this be happening?
First of all make sure you connect to port 22. Then make sure your operating system's firewall doesn't block incoming connections to port 22. In case the address you have configured for the server is "localhost" and try to connect from another machine you won't be able to connect. In that case you shoud use "" as ip address.
Related
I'm working on Python 2.7 code to read a value from HTML page by using urllib2 library. I want to timeout the urllib2.urlopen function after 5 seconds in case of no Internet and jump to remaining code.
It works as expected when computer is connected to working internet connection. And for testing if I set timeout=0.1 it timed out suddenly without opening url, as expected. But when there is no Internet, timeout not works either I set timeout to 0.1, 5, or any other value. It simply does not timed out.
This is my Code:
import urllib2
url = "https://alfahd.witorbit.net/fingerprint.php?n"
try:
response = urllib2.urlopen(url , timeout=5).read()
print response
except Exception as e:
print e
Result when connected to Internet with timeout value 5:
180
Result when connected to Internet with timeout value 0.1 :
<urlopen error timed out>
Seems timeout is working.
Result when NOT connected to Internet and with any timeout value (it timed out after about 40 seconds every time I open url despite of any value I set for timeout=:
<urlopen error [Errno -3] Temporary failure in name resolution>
How can I timeout urllib2.urlopen when there is no Internet connectivity? Am I missing some thing? Please guide me to solve this issue. Thanks!
Because name resolution happens before the request is made, it's not subject to the timeout. You can prevent this error in name resolution by providing the IP for the host in your /etc/hosts file. For example, if the host is subdomain.example.com and the IP is 10.10.10.10 you would add the following line in the /etc/hosts file
10.10.10.10 subdomain.example.com
Alternatively, you may be able to simply use the IP address directly, however, some webservers require you use the hostname, in which case you'll need to modify the hosts file to use the name offline.
Tried to create a local server to broadcast messages in python using the websocket module. (Mac OSX)
from websocket import create_connection
url = 'ws://localhost:8888/'
ws = create_connection(url)
ws.send("helloworld")
ws.close()
Saw this error: ConnectionRefusedError: [Errno 61] Connection refused
Your port 8888 is either closed or filtered. You most port forward it in your router.
Refer to this guide.
I configured my windows machine's DNS server to 127.0.0.1 and on localhost I created a basic python server:
from socket import *
serverPort = 53
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('127.0.0.1', serverPort))
print "The server is ready to receive on port: {}".format(serverPort)
while 1:
try:
message, clientAddress = serverSocket.recvfrom(512)
except:
continue
print clientAddress, message
modifiedMessage = "127.0.0.1"
serverSocket.sendto(modifiedMessage, clientAddress)
PS :I know that DNS is a binary protocol and sending ASCII text won't do any good, but I am not trying to make a resolver, I am trying to see with transperancy that how the former works.
When I srarted the server, I am greated with the following output:
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 53945), '.\x9c\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 50065), '\xb5\xfc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 61362), '\xefc\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01')
(('127.0.0.1', 52718), '\xc7\x15\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x05tiles\x08services\x07mozilla\x03com\x00\x00\x01\x00\x01')
But unlike as I enticipated, I am still able to open websites. And Wireshark told me that I am making connection to 8.8.8.8(IDK how?).
I tried flushing the DNS cashe from my machine, nada.
What am I missing?
PPS: If I remove the try/catch clause I get this error(a few seconds after the execution of the program):
error: [Errno 10054] An existing connection was forcibly closed by the remote host
You probably have configured Googles 8.8.8.8 as a fallback DNS server.
And since you are destroying the DNS answers, whoever is receiving these broken answers is falling back to the secondary server. The whole path of DNS queries on a typical UN*X machine is quite complicated and the whole system is usually quite robust.
I want to use this code to connect to S3
conn = S3Connection('<aws access key>', '<aws secret key>')
But I then receive an error: socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
The reason may be that I am behind a proxy server, so I need to know the IP address of the connection in order to open ports.
So the question is, how can I know that IP, or url ( I know that the IP may vary for each connection).
I solved my problem by adding proxy authentication to S3Connection and everything works as expected
This question already has an answer here:
Read a list of hostnames and resolve to IP addresses
(1 answer)
Closed 1 year ago.
I have create a script to resolve IP to hostname. The script does not resolve the hostname, it gives the following error:
cannot resolve hostname: 10.10.10.10
[Errno 11004] getaddrinfo failed
cannot resolve hostname: 10.10.10.10 [Errno 11004] getaddrinfo failed
Please suggest. I'm new to python. The text file contains more than 1000 IPs.
#!/usr/bin/python
import socket
pfile = open ('C:\\Python27\\scripts\\test.txt')
while True:
IP = pfile.readline()
if not IP:
break
try:
host = socket.gethostbyaddr("IP")
print host, IP
except socket.gaierror, err:
print "cannot resolve hostname: ", IP, err
pfile.close()
There are two problems here.
First, as FatalError pointed out, you're not looking up the value of the IP variable, but the string "IP".
Second, pfile.readline() is going to leave a trailing newline at the end of the IP string, so it's still going to fail.
So:
host = socket.gethostbyaddr(IP.rstrip())
Also, on some platforms, if your DNS isn't working, gethostbyaddr will fail even when given an IP address. So, you may want to do a simple test on the machine you're running the script on (if it's not the same machine you're already using for SO)—e.g., open a browser and go to Google.
As far as I can tell, there are different problems.
The line:
host = socket.gethostbyaddr("IP")
will fail because of the string. To fix this, use host = socket.gethostbyaddr(IP).
Furthermore, the error you posted here is caused by 10.10.10.10 being a private IP. The ranges 10.0.0.0–10.255.255.255, 172.16.0.0–172.31.255.255 and 192.168.255.255 are private network blocks; socket.gethostbyaddr() is not able to resolve these addresses. See https://www.rfc-editor.org/rfc/rfc1918 for more information about private blocks.
After a little Googling, I got it to work in Python 3 as follows:
import socket
pfile = open ('C:\\TEMP\\IPs.txt')
while True:
IP = pfile.readline()
try:
host = socket.gethostbyaddr(IP.rstrip())
print(IP,host)
except Exception as e:
print(IP,"NULL")
pfile.close()
This works:
import socket
IP = "www.google.ca"
host = socket.gethostbyaddr(IP)
print host, IP