The whois.whois function always gets a timed out error - python

The whois.whois function always gets a timed out error.
At first, I thought it was because my project is written in Python 2.7 but I also checked in 3.7 and got the same error.
I checked the address on the online website that uses whois and the link worked and didn't get this error.
Anyone knows why this is happening?
import whois
w = whois.whois("https://stackoverflow.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python37\lib\site-packages\whois\__init__.py", line 43, in whois
text = nic_client.whois_lookup(None, domain.encode('idna'), flags)
File "C:\Program Files\Python37\lib\site-packages\whois\whois.py", line 264, in whois_lookup
result = self.whois(query_arg, nichost, flags)
File "C:\Program Files\Python37\lib\site-packages\whois\whois.py", line 142, in whois
s.connect((hostname, 43))
socket.timeout: timed out

Your code has at least two problems, and you may have a network problem also.
However, there is no reason for it not to work on Python2.
About the code
This works perfectly fine:
In [7]: import whois
In [8]: print whois.query('stackoverflow.com').expiration_date
2020-02-02 11:59:59
Note two things:
whois is about domain names, not URLs; so you should pass a domain name; note more generally that for new endeavors you should have a look at RDAP instead of whois since you will get a far better experience
you need to use whois.query not whois.whois (you are not saying which version of the library you use, but at its documentation page on https://pypi.org/project/whois/ you can clearly see it is whois.query so I do not know where your whois.whois` comes from).
About the network
You show a network error. It is not 100% clear but you may or may not have access to the whois servers you want to query.
Easy way to test: just use the command line whois from the same box as your code (but again use a domain name, not a URL as parameter) and you will see what is happening.
You can even do directly a telnet on port 43 as whois does nothing else.
$ echo 'stackoverflow.com' | nc whois.verisign-grs.com 43 | grep 'Expiry'
Registry Expiry Date: 2020-02-02T11:59:59Z

Related

Python Foreman Issue

Python Version = 3.9,
Foreman Version = 2.5.4
I'm using the Python Foreman (python-foreman) module to connect to Foreman to do a bunch of automation tasks, however as soon as i'm connecting to Foreman as below:
from foreman.client import Foreman
f = Foreman('http://foreman-url.com/api/hosts', ('admin', 'password'), api_version = 2)
I'm getting a bunch of conflicting statements as below and eventually erroring out.
There is a conflict trying to redefine a method for a foreign resource (foreman_tasks_index):
resource:
apipie_resource: foreman_tasks
new_api: <resource:, name:foreman_tasks_index>
new_url: /foreman_tasks/api/tasks/:parent_task_id/sub_tasks
old_api: <resource:, name:foreman_tasks_index>
old_url: /foreman_tasks/api/tasks
There is a conflict trying to redefine a method for a foreign resource (sync_plans_rganizations):
resource:
apipie_resource: sync_plans
new_api: <resource:organizations, name:sync_plans_rganizations>
new_url: /katello/api/organizations/:organization_id/sync_plans/:id/add_products
old_api: <resource:organizations, name:sync_plans_rganizations>
old_url: /katello/api/organizations/:organization_id/sync_plans/:id
Eventually the script is erroring out at this:
Traceback (most recent call last):
File "", line 1, in
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 619, in init
self._generate_api_defs(use_cache, strict_cache)
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 820, in generate_api_defs
new_resource, extra_foreign_methods = parse_resource_definition(
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 375, in parse_resource_definition
functions[api.name] = api.generate_func()
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 282, in generate_func
six.exec(code)
File "", line 1
def lifecycle_environments_update(self, id, organization_id=None, new_name=None, description=None, registry_name_pattern=None, registry_unauthenticated_pull=None, async=None):
^
SyntaxError: invalid syntax
Can you pls help with this or let me know the best way to connect to Foreman API via Python to carry out the automations?
The error message you are getting states that (sine Python 3.5) async is a reserved keyword, and cannot be a variable name. This means that the version of Foreman you are using is incompatible with Python 3.9. Try updating Foreman, downgrading Python, or (if you are comfortable doing it) try to fix the issue yourself (changing the variable name from async to something else, and hoping it works).
I am not sure whether the offending code comes directly from Foreman. Try to search your code (and libraries) for async=None pattern, as it may also not be Foreman's fault.

dnf.base.fill_sack() -- how to use the certs in yum.conf?

I have cert information for using HTTPS in my repos stored in /etc/yum.conf at the bottom:
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
...
sslclientcert=/path/to/cert.pem
sslclientkey=/path/to/privatekey.pem
sslcacert=/path/to/ca.pem
When I use dnf via the terminal, it can communicate with the repos and retrieve repodata/repomd.xml (and package information and all) just fine. However, when I do it via python:
import dnf
with dnf.Base() as base:
base.read_all_repos()
base.fill_sack()
I get:
Errors during downloading metadata for repository '<reponame>':
- Curl error (60): Peer certificate cannot be authenticated with given CA certificates for <repo-path>/repodata/repomd.xml [SSL certificate problem: self signed certificate in certificate chain]
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/dnf/repo.py", line 574, in load
ret = self._repo.load()
File "/usr/lib64/python3.6/site-packages/libdnf/repo.py", line 397, in load
return _repo.Repo_load(self)
libdnf._error.Error: Failed to download metadata for repo '<reponame>': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/dnf/base.py", line 399, in fill_sack
self._add_repo_to_sack(r)
File "/usr/lib/python3.6/site-packages/dnf/base.py", line 139, in _add_repo_to_sack
repo.load()
File "/usr/lib/python3.6/site-packages/dnf/repo.py", line 581, in load
raise dnf.exceptions.RepoError(str(e))
dnf.exceptions.RepoError: Failed to download metadata for repo '<reponame>': Cannot download repomd.xml: Cannot download repodata/repomd.xml: All mirrors were tried
How I know it's an SSL/HTTPS problem:
Implemented HTTPS on my personal repo a few days ago, and that's when the issues started (it worked just fine prior to that)
If I change the URL to http:// instead of https://, it works fine (but this isn't a viable solution).
If I run the dnf via commandline, it also works just fine.
I know that dnf has the ability to pull those certs in from the yum.conf file, but does anyone know (or can figure out) how it's actually done? I've spent a good while digging through the code now and can't figure it out (I'm not particularly familiar with how swig works, which is what I'm getting caught up on witin the dnf code itself).
Any help is appreciated.
Figured it out a little while ago, so maybe this'll help someone else out there:)
import dnf
from libdnf.conf import Option
with dnf.Base() as base:
# Read in the config from yum.conf
with open('/etc/yum.conf') as inf:
yum_config = inf.read()
# Split the config into a dictionary
yum_settings = yum_config.split('\n')
yum_settings = {k.split('=')[0]:k.split('=')[1] for k in yum_settings in '=' in k}
# The settings below will require a priority. This is the third highest one out of about 10, so I went with this one
priority = Option.Priority_DROPINCONFIG
base.conf._config.sslclientcert().set(priority, yum_settings['sslclientcert'])
base.conf._config.sslclientkey().set(priority, yum_settings['sslclientkey'])
base.conf._config.sslcacert().set(priority, yum_settings['sslcacert'])
base.read_all_repos()
base.fill_sack()
And now it works! It uses the SSL certs in the files in /etc/yum.conf to connect to the repos and all is well!

Could not get username from env

Thu, 06 May 2021 13:49:05 Desmond Driver v2.3
Traceback (most recent call last):
File "/opt/schrodinger2021-1/internal/bin/desmond_driver.py", line 294, in <module>
main(sys.argv[1:])
File "/opt/schrodinger2021-1/internal/bin/desmond_driver.py", line 279, in main
driver_model = get_driver_model(option.destrier_flag, backend)(args,
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 426, in __init__
DriverModel.__init__(self, args, backend)
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 118, in __init__
self._config_cuda()
File "/opt/schrodinger2021-1/internal/bin/drivermodel.py", line 175, in _config_cuda
tmp_dir = fileutils.get_directory_path(fileutils.TEMP)
File "/opt/schrodinger2021-1/internal/lib/python3.8/site-packages/schrodinger/utils/fileutils.py", line 669, in get_directory_path
return mm.get_schrodinger_temp_dir()
**RuntimeError: could not get username from env**
Hi there, I tried to install a molecular dynamic package on collab called Desmond. The installation looks fine, even the -h flag shows a good return. but when I try to run some real job, there the error showed up saying could not get the username from env. Not sure what is going on, please help if you have any ideas, many thanks.
try this
import getpass
getpass.getuser()
The current user name will be returned by this.
eg
>>> import getpass
>>> print(getpass.getuser())
root
>>>
I just figured it out after two months!
The trick is, unlike a local computer or an HPC cluster, the way of cloud platform does not define the USER environment variable, yes, it is as simple as the error shows to some extent.
Just do: (take the Baidu AI studio as example)
echo “export USER=aistudio” >> ~/.bashrc
source ~/.bashrc
that’s it, and then, desmond will be able to up and running, I believe on Google Colab, the solution should be very similar.
The best way to find the correct USER value for your system is use "printenv" and look for the $HOME variable, your username should be as part of $HOME, then you just copy that name and give it to the new $USER variable.

How do I put a ESXi host into maintenance mode using pyvmomi?

I was asked to write some python code that would put an VMWare ESXi host into maintenance mode. I was given the name of a virtual center, test-vc, and the hostname of an ESXi host, test-esxi-host and this link ...
https://github.com/vmware/pyvmomi/blob/master/docs/vim/HostSystem.rst
... which provides some documentation on the method I am suppose to use, EnterMaintenanceMode(timeout, evacuatePoweredOffVms, maintenanceSpec).
I am really a complete loss as to what to do really and could use some help. I have tried doing this from a python console:
from pyVmomi import vim
vim.HostSystem.EnterMaintenanceMode(timeout=0)
Which results in this error trace:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/apps/cpm/red/env/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 574, in __call__
return self.f(*args, **kwargs)
TypeError: _InvokeMethod() takes at least 2 arguments (1 given)
Also I am a kind of confused about how the EnterMaintenanaceMode routine would know that I want to put the host test-esxi-host in virtual center test-vc?
Update: I think I have figured it out. Here's what I think I need to do:
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit
si = SmartConnectNoSSL(host=vc_host, user=user, pwd=pwd)
cont = si.RetrieveContent()
atexit.register(Disconnect, si) # maybe. I am not really sure what this does
objview = si.content.viewManager.CreateContainerView(si.content.rootFolder, [vim.HostSystem], True)
objview.view[0].EnterMaintenanceMode(0)
Of course the line
objview.view[0].EnterMaintenanceMode(0)
is sure to wreak havoc as I have no idea if that is the host, 'test-esxi-host', I want to put into maintenance mode. I guess I could do this
for h in objview.view:
if h.name == 'test-esxi-host'
h.EnterMaintenanceMode(0)
I hope there is a better way to do the above. Something like
get_host(objview.view, 'test-esxi-host').EnterMaintenanceMode(0)
Have a look at Getting started with VMwares ESXi/vSphere API in Python.
To get a VM object or a list of objects you can use the searchIndex
class. The class had methods to search for VMs by UUID, DNS name, IP
address or datastore path.
Hopefuly, there are a couple of ways to look for objects in vCenter:
FindByUuid (VM|Host)
FindByDatastorePath (VM)
FindByDnsName (VM|Host)
FindByIp (VM|Host)
FindByInventoryPath (managed entity: VM|Host|Resource Pools|..)
FindChild (managed entity)
Many of these also have FindAll.. methods which allow a much broader look up.
For this particular case, you could use FindByDnsName to look for your host.
searcher = si.content.searchIndex
host = searcher.FindByDnsName(dnsName='test-esxi-host', vmSearch=False)
host.EnterMaintenanceMode(0)
This code requires you to authenticate to vCenter (#SmartConnectNoSSL) with a user having Host.Config.Maintenance privileges.
Finally you can take your host out of maintenance mode with: host.ExitMaintenanceMode(0)

PyMongo - Clone Collection from another server

I'm attempting to clone a collection on a remote server to my localhost with indexs using Python (as I'll have to be automating multiple tasks with this function using Python later on). From what I've read, using db.command would be the best way to approach this, but I can't seem to get it working, or even find what I'm really doing wrong.. Here's my code and error, any help is appreciated.
client = MongoClient()
db = clientProd.tools
colc = db.newDump
db.command({"cloneCollection": "databaseName.dump", "from": "example.com:6001"})
Error:
Traceback (most recent call last):
File "/Users/dustin/Git/redbull_dev/test.py", line 14, in <module>
dbProd.command({"cloneCollection": "databaseName.dump", "from": "example.com:6001"})
File "/Library/Python/2.7/site-packages/pymongo-2.6.3-py2.7-macosx-10.8-intel.egg/pymongo/database.py", line 396, in command
File "/Library/Python/2.7/site-packages/pymongo-2.6.3-py2.7-macosx-10.8-intel.egg/pymongo/helpers.py", line 147, in _check_command_response
pymongo.errors.OperationFailure: command {'from': 'example.com:6001', 'cloneCollection': 'databaseName.dump'} failed: no such cmd: from
[Finished in 5.2s with exit code 1]
Apparently you looked at the doc from mongo, you should look at the doc from pymongo http://api.mongodb.org/python/current/api/pymongo/database.html
From that, the correct command should be:
db.command("cloneCollection", collection="databaseName.dump", from="example.com:6001")
Edit
Since from is a python keyword, we probably need to create a dict and unpack it:
db.command("cloneCollection", **{'collection': "databaseName.dump", 'from': "example.com:6001"})
I found that the selected solution didn't quite work (maybe version issue), this is my solution:
db_dest.command("cloneCollection", **{"cloneCollection": db_str + ".Sentence",
'collection': db_str + ".Sentence",
'from': mongodb_ip + ":27017"})

Categories

Resources