I am trying to create a telnet to Teamspeak 3 Server Query through python, this is my code:
__author__ = 'Khailz'
import telnetlib, socket
HOST = "unlightedgaming.com"
port = 10011
timeout = 5
for i in range(len(HOST)):
print "scanning " + HOST[i] + " ...\n"
try:
tn = telnetlib.Telnet(HOST[i],23,3)
except socket.timeout:
pass
#tn = telnetlib.Telnet(host, port, timeout)
#telnetlib.Telnet(host, port, timeout)
tn.read_until("Welcome to the TeamSpeak 3 ServerQuery interface")
print tn.read_all()
But i seem to get the error from socket saying that it cannot get the address.
C:\Python27\python.exe C:/Users/Khailz/PycharmProjects/Teamspeak-IRC/test.py
scanning 1 ...
Traceback (most recent call last):
File "C:/Users/KhailzXsniper/PycharmProjects/Teamspeak-IRC/test.py", line 14, in <module>
tn = telnetlib.Telnet(HOST[i],23,3)
File "C:\Python27\lib\telnetlib.py", line 211, in __init__
self.open(host, port, timeout)
File "C:\Python27\lib\telnetlib.py", line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed
What am I doing wrong
For each character in HOST, you are attempting to resolve that character; this is due to your use of a for loop in combination with indexing HOST. For example in the first iteration you attempt to connect to a host with an I.P. of "1"- that is what produces the gaierror.
To fix it, just don't attempt to connect to a specific index of HOST: get rid of HOST[i] and replace it with HOST alone in the call to telnetlib.Telnet:
tn = telnetlib.Telnet(HOST, 23, 3)
As to what you're trying to do with that loop, however, I'm baffled.
Related
I am getting an error when i try to use the attached python code to connect and send messages to the Things Network application. When i try using the mqtt mosquitto broker in the terminal with
Traceback (most recent call last):
File "/Users/ayushsharma/Desktop/ttn.py", line 28, in <module>
client.connect(broker,port = 8883)
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/Users/ayushsharma/Desktop/ven/lib/python3.10/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 824, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 955, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
it works in connecting but not through the python script. I'm really new to all this so i appreciate any feedback or guidance on how to approach this problem of retrieving date from TheThingsNetwork.
#!/usr/bin/env python3
import paho.mqtt.client as paho
#settings
app_id = "appID"
access_key = "access_key"
broker="nam1.cloud.thethings.network "
port=1883
def on_publish(client,userdata,result):
print("data published \n")
print(client,userdata,result)
pass
def on_log(mqttc, obj, level, string):
print(string)
#setup
client = paho.Client()
client.username_pw_set(app_id,access_key)
#callbacks
client.on_publish = on_publish
client.on_log = on_log
#establish connection
client.connect(broker,port = 8883)
client.loop_start()
#publish
ret= client.publish("appID","on",qos=1)
ret.wait_for_publish()
[1]: https://i.stack.imgur.com/2oejU.png
Here is my code:
import smtplib
connection = smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user="mymail#gmail.com", password="mypassowrd")
connection.sendmail(from_addr="mymail#gmail.com", to_addrs="recievermail#gmail.com", msg="Hello")
connection.close()
So I am getting this error:
Traceback (most recent call last):
File "E:\100Days-Python_programs\Day32\Birthday Wisher (Day 32) start\main.py", line 3, in <module>
connection = smtplib.SMTP("smtp.gmail.com")
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process finished with exit code 1
I turned on Less secure app access:
I turned off every security steps too:
And I also turned off the firewall protection as well.
But Nothing worked.
So please someone help me.
You need to specify the port. In this case, it's 587 for TSL.
Somehow it works, but I don't have profound knowledge to explain why.
I had the same problem, so there is a solution:
connection = smtplib.SMTP("smtp.gmail.com", 587)
I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?
But, my the mails that I sent do not reach the addresses.
This is the error that I get:
Traceback (most recent call last):
File "something.py", line 24, in <module>
server = smtplib.SMTP('myserver')
File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
What should I be doing here?
What you've get is a DNS query error indicating that domain myserver does not exist.
You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.
This is how I do it.
import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email#gmail.com','your_password')
server.sendmail('your_email#gmail.com','your_email#gmail.com','test email')
pwd = "mypassword"
import smtplib
server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113#gmail.com",pwd)
msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113#gmail.com", "sender#xyz.com", msg)
server.quit()
I have tried sending a mail through python...
Error :
Traceback (most recent call last):
File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
server = smtplib.SMTP('smtp.gmail.com')
File "C:\Python27\lib\smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
I am connecting through a proxy connection
I set the proxy through cmd in windows.
Please help me with this.
Update :
I am sure with the internet connection :
import urllib2
def internet_on():
try:
response=urllib2.urlopen('https://www.google.co.in',timeout=1)
return True
except urllib2.URLError as err: pass
return False
print internet_on()
Output is True
Your code works fine for me, so it's probably the connection settings.
Try changing server to:
server = smtplib.SMTP('64.233.184.108')
(that's the IP address of smtp.gmail.com, to bypass DNS resolution)
Try updating the server = smtplib.SMTP('smtp.gmail.com') to include a port. The port for gmail is 587, so add that as a second parameter: server = smtplib.SMTP('smtp.gmail.com', 587)
If this does not work, make sure that you have used this site to make sure that the account will let you in:
https://www.google.com/settings/security/lesssecureapps
I'm trying telnet to routers based on ip addresses from hosts.txt file via python3 script. However I receive the following error:
$ ./telnet_group_1.py
10.1.1.1
Traceback (most recent call last):
File "./telnet_group_1.py", line 23, in <module>
tn = telnetlib.Telnet(host)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/telnetlib.py", line 209, in __init__
self.open(host, port, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/telnetlib.py", line 225, in open
self.sock = socket.create_connection((host, port), timeout)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/socket.py", line 386, in create_connection
for res in geta ddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
If I use the same ip address but defined into the script (not taken from hosts.txt file), script works fine. Please advise how to change the code to make it work ?
Script details:
import telnetlib, socket
hosts = open("hosts.txt", 'r')
output = open("output.txt", 'w')
username = 'admin'
password = 'admin'
for h in hosts:
host_s = str(h)
tn = telnetlib.Telnet(host_s)
tn.read_until(b'Username: ')
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b'Password: ')
tn.write(password.encode('ascii') + b"\n")
tn.write(b"terminal len 0\n")
tn.write(b"show version\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'), file = output)
hosts.txt consists only of a single entry as of now - just to make it work:
10.1.1.1
You want to use .strip() on the lines from the hosts file; a newline is included otherwise:
for h in hosts:
tn = telnetlib.Telnet(h.strip())