How to get friendly domain name from ip address in python? - sockets

I am a networking and python novice.
I am going to get domain names from IP addresses and vice versa.
Here is my code snippet, which is very famous on stackoverflow.com.
import socket
print(socket.gethostbyname('stackoverflow.com')) #151.101.65.69
print(socket.gethostbyaddr('151.101.65.69')) #error
151.101.65.69
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
socket.gethostbyaddr("151.101.129.69")
socket.herror: [Errno 11004] host not found
In case of google.com I get this result:
import socket
print(socket.gethostbyname("google.com")) #'142.251.43.14'
print(socket.gethostbyaddr("142.251.43.14")) #('tsa03s08-in-f14.1e100.net', [], ['142.251.43.14'])
The output as
'142.251.43.14'
('tsa03s08-in-f14.1e100.net', [], ['142.251.43.14'])
I want to get a friendly domain name like "stackoverflow.com" and "google.com".
Is there anyone who has a similar experience to me?

Related

Error trying to send message to serve Ubuntu 18.04 LTS

When I try to send a quoted string from client to server, it works. However, when i try to send a variable storing the input of the user to the server, it does not. Anybody knows why?
server_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind( ("0.0.0.0", 1234) )
buff, addr = s.recvfrom(100)
print buff, addr
client_file
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nume_user = input()
# s.sendto( nume_user, ("127.0.0.1", 1234) ) # this does not work
s.sendto("john", ("127.0.0.1", 1234) ) # this works
buff, addr = s.recvfrom(100)
print buff
This is the error that I am getting ( Ubuntu 18.04 LTS )
Traceback (most recent call last:
File "c4-1.py", line 5, in <module>
nume_user = input()
File "<string>", line 1, in <module>
NameError: name 'ionut' is not defined
From the documentation:
input([prompt])
Equivalent to eval(raw_input(prompt)).
Thus it will read the string you enter (in this case ionut) and then eval it. Since ionut is not a declared variable or otherwise valid Python statement it will throw the error shown.
Also from the documentation:
Consider using the raw_input() function for general input from users.
This is what you should use instead and then you don't get the error.
Apart from that consider using Python3 instead of Python2 which you currently use. Python2 is end of life and also the input function in Python3 is more what you seem to expect - see this documentation.

Sending HTTP post requests

I am using a micropython firmware version esp8266-20190125-v1.10.bin for esp8266mod. I accessed REPL prompt via a wired connection using picocom and connected the device to my home wifi. I was trying to send some HTTP post requests using urequests.
import urequests
response = urequests.post('http://lakshmick.pythonanywhere.com/savedata', data = {'string': 'posting from micropython'})
I got some error like this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "urequests.py", line 115, in post
File "urequests.py", line 100, in request
File "urequests.py", line 79, in request
TypeError: object with buffer protocol required
Does it require any additional configuration procedures before importing urequests?
You need to serialize the data into a string before sending it:
myPostedData = {'YYYY' : 100}
res = urequest.post('https://XXX.amazonaws.com/XXX/XXX', data=json.dumps(myPostedData))
jsonresults = json.loads(res.content)

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. tastypie

Continuing my search for an answer to get oauth2.0 to work on pythonanywhere.
i am following this tutorial: http://ianalexandr.com/blog/building-a-true-oauth-20-api-with-django-and-tasty-pie.html
im using django 1.6 : https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango
when i get to this line of codes:
from provider.oauth2.models import Client
# from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
User = get_user_model()
u = User.objects.get(id=1)
c = Client(user=u, name="mysite client", client_type=1, url="http://pythonx00x.pythonanywhere.com")
c.save()
c.client_id
'd63f53a7a6cceba04db5'
c.client_secret
'afe899288b9ac4127d57f2f12ac5a49d839364dc'
it seems that i got an error at line:
User = get_user_model()
and it raise an error:
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
here is the full stack trace:
Traceback (most recent call last):
File "addClient.py", line 9, in <module>
User = get_user_model()
File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 136, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL)
File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/apps/registry.py", line 200, in get_model
self.check_models_ready()
File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/apps/registry.py", line 132, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
I can't seem to find out how to get the models load.
if I'm getting the idea right.
I think you may not be using the version of Django that you think you are. AppRegistryNotReady was introduced in Django 1.7. I would guess that, if you pinned your DJango version to 1.6, your code would work.

Python notebook fail to load properly

I am using the Anaconda 2.76 version. It was working fine until today. The notebook page was not loaded properly. Noe of the feature was responsive. After I did some research, I think it is some coding error, but since I am really not a computing kind of guy, I don't know where exactly went wrong and how to fix it. Below is the error message I received. please lend me a hand. Thanks a lot.
HTTPRequest (protocol=’http’, host =’127.0.0.1:8888;,method=’GET’,uri=’/static/base/images/favicon.ico’,version=’HTTP/1.1’,remote_ip-‘127.0.0.1’,headers={‘connection’:’keep-alive’,’Accept-Language’:’zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4’,’Accept-Encoding’:’gzip,deflate,sdch’,'host’:'127.0.0.1:8888’,Accept':'*/*','User-Agent':'Mozilla/5.0(windows NT 6.1; WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/34.0.1847.131 Safari/537.36'})
Traceback (most recent call last):
File "D:|Anaconda\lib\site-packages\tornado\web.py", line 1218, in _when_complete
callback()
File "D:|Anaconda\lib\site-packages\tornado\web.py", Line 1239, in _execute_method
self._when_coplete(method(*self.path_args,**self.path_kwargs),
File "D:|Anaconda\lib\site-packages\IPython\html\base\handlers.py", line 318, in get
mime_type, encoding=mimetypes.guess_type(abspath)
File "D:\Anaconda\lib\mimetypes.py", line 297, in guess_type
init()
File "D:\Anaconda\lib\mimetypes.py", line 358,in init
db.read_windows_registry()
File "D:\Anaconda\lib\mimetypes.py", line 258,in read_windows_registry
for subkeyname in enum_types(hkcr):
File "D:\Anaconda\lib\mimetypes.py", line 249,in enum_types
ctype=ctype.encode(default_encoding)#omit in 3.X!
UnicodeDecodeError: "ascii" codec can't decode byte 0*b0 in position 1:ordinal not in range(128)
2014-5-12 16:43:45.456 [tornado.access] ERROR |500 GET /static/base/images/favicon.ico (127.0.0.1) 97.00ms`
This is a known issue.
I've solved the same problem using the following temporary modification of Anaconda/Lib/mimetypes.py, lines 252-253 (as proposed here).
try:
ctype = ctype.encode(default_encoding) # omit in 3.x!
except UnicodeEncodeError:
pass
except Exception: #<--
pass #<--
else:
yield ctype

Bottle web framework: any way to run a console/shell and get it to work with Werkzeug?

I searched but couldn't find an easy way to run a console or shell akin to Django's manage.py shell or Rail's rails console
Since I just started using Bottle for an existing project, I just wanted to play around with the existing models and managers in the console. The closest I came up with was using ipdb's set_trace() and go from there, but that's not ideal by any means.
Also, I tried integrating Bottle with Werkzeug, but when I follow the instructions:
import bottle
app = bottle.Bottle()
werkzeug = bottle.ext.werkzeug.Plugin()
app.install(werkzeug)
I get the following traceback error:
Traceback (most recent call last):
File "mysite.py", line 62, in <module>
werkzeug = bottle.ext.werkzeug.Plugin()
AttributeError: 'module' object has no attribute 'werkzeug'
Try adding importing bottle.ext.werkzeug by adding this at the beginning of your source:
import bottle.ext.werkzeug