python libcloud vcloud connection - python

I am trying to connect to Vcloud using lib cloud, the authentication works fine using rest API from firefox, but fails in python. am I missing something?
__author__ = 'kshk'
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
import libcloud.security
def testConnection():
#libcloud.security.VERIFY_SSL_CERT = False
vcloud = get_driver(Provider.VCLOUD)
driver = vcloud("login", "passwd", host = "https://portal.vcloud")
nodes = driver.list_nodes()
print nodes
def main():
testConnection()
if __name__ == "__main__":
main()
OUTPUT:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/krishnaa/PycharmProjects/VCloud-API/vcloud_test.py
Traceback (most recent call last):
File "/Users/krishnaa/PycharmProjects/VCloud-API/vcloud_test.py", line 21, in <module>
main()
File "/Users/krishnaa/PycharmProjects/VCloud-API/vcloud_test.py", line 18, in main
testConnection()
File "/Users/krishnaa/PycharmProjects/VCloud-API/vcloud_test.py", line 14, in testConnection
nodes = driver.list_nodes()
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/vcloud.py", line 559, in list_nodes
return self.ex_list_nodes()
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/vcloud.py", line 573, in ex_list_nodes
vdcs = self.vdcs
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/vcloud.py", line 407, in vdcs
self.connection.check_org() # make sure the org is set.
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/vcloud.py", line 325, in check_org
self._get_auth_token()
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/compute/drivers/vcloud.py", line 835, in _get_auth_token
headers=self._get_auth_headers())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 829, in _send_output
self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 791, in send
self.connect()
File "/Library/Python/2.7/site-packages/apache_libcloud-0.15.1-py2.7.egg/libcloud/httplib_ssl.py", line 96, in connect
self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Your code doesn't work because the "host" argument expects just a hostname and not a url.
If you change it to:
driver = vcloud("login", "passwd", host="portal.vcloud")
it should work.

Related

HTTPS request with Python standard library

UPDATE: I managed to do a request with urllib2, but I'm still wondering what is happening here.
I would like to do a HTTPS request with Python.
This works fine with the requests module, but I don't want to use external dependencies, so I'd like to use the standard library.
httplib
When I follow this example I don't get a response. I get a timeout instead. I'm out of ideas as to what would cause this.
Code:
import requests
print requests.get('https://python.org')
from httplib import HTTPSConnection
conn = HTTPSConnection('www.python.org')
conn.request('GET', '/index.html')
print conn.getresponse()
Output:
<Response [200]>
Traceback (most recent call last):
File "test.py", line 6, in <module>
conn.request('GET', '/index.html')
File "C:\Python27\lib\httplib.py", line 1069, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1109, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 1065, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 892, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 854, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1282, in connect
HTTPConnection.connect(self)
File "C:\Python27\lib\httplib.py", line 831, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 575, in create_connection
raise err
socket.error: [Errno 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
urllib
This fails for a different (but possibly related) reason. Code:
import urllib
print urllib.urlopen("https://python.org")
Output:
Traceback (most recent call last):
File "test.py", line 10, in <module>
print urllib.urlopen("https://python.org")
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 215, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 445, in open_https
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 1065, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 892, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 854, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 1290, in connect
server_hostname=server_hostname)
File "C:\Python27\lib\ssl.py", line 369, in wrap_socket
_context=self)
File "C:\Python27\lib\ssl.py", line 599, in __init__
self.do_handshake()
File "C:\Python27\lib\ssl.py", line 828, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:727)
What is requests doing that makes it succeed where both of these libraries fail?
requests.get without timeout parameter mean no timeout at all.
httplib.HTTPSConnection accept parameter timeout in Python 2.6 and newer according to httplib docs. If your problem was caused by timeout, setting high enough timeout should help. Please try replacing:
conn = HTTPSConnection('www.python.org')
with:
conn = HTTPSConnection('www.python.org', timeout=300)
which will give 300 seconds (5 minutes) for processing.

connection error, from python in RCMES vagrant

I am trying to test python in RCMES vagrant, and I am getting connection error. can anyone please help to solve this
(OCW)vagrant#precise64:~/RCMES/test$ python test.py
Traceback (most recent call last):
File "test.py", line 49, in <module>
urllib.urlretrieve(FILE_LEADER + MODEL, MODEL)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/urllib.py", line 350, in open_http
h.endheaders(data)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/httplib.py", line 897, in _send_output
self.send(msg)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/httplib.py", line 859, in send
self.connect()
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/httplib.py", line 836, in connect
self.timeout, self.source_address)
File "/home/vagrant/miniconda2/envs/OCW/lib/python2.7/socket.py", line 575, in create_connection
raise err
IOError: [Errno socket error] [Errno 110] Connection timed out

Unable to download an image via urlretrieve() function

I am on Ubuntu 16.10. I am trying to download an image using urlretrieve() function of python. I am getting the following errors.
File "downloadImage.py", line 9, in <module>
downloadWebImage("https://qph.ec.quoracdn.net/main-qimg-601accaf841bba9145d3c46bece1ea10-p")
File "downloadImage.py", line 7, in downloadWebImage
urllib.urlretrieve(url, fullName)
File "/usr/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 897, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 859, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1278, in connect
server_hostname=server_hostname)
File "/usr/lib/python2.7/ssl.py", line 353, in wrap_socket
_context=self)
File "/usr/lib/python2.7/ssl.py", line 601, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 830, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:590)
Also my code is :
import random
import urllib
def downloadWebImage(url):
name = random.randrange(1,1000)
fullName = str(name) + '.jpg'
urllib.urlretrieve(url, fullName)
downloadWebImage("https://qph.ec.quoracdn.net/main-qimg-601accaf841bba9145d3c46bece1ea10-p")
So what are the errors, why are they occuring? What is their fix?

Python scraping script error

I'm trying to run the following program, following a tutorial. How should I fix the error shown below?
import re
import urllib
#name=raw_input('Enter URL for the site name:')
handle=urllib.urlopen('http://www.iitk.ac.in')
for line in handle:
print line.strip()
Error and stack trace:
Traceback (most recent call last):
File "second.py", line 5, in <module>
handle=urllib.urlopen('http://www.iitk.ac.in')
File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 350, in open_http
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 1048, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 892, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 854, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 831, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno -2] Name or service not known

cant connect to TOR with python [duplicate]

This question already has answers here:
"getaddrinfo failed", what does that mean?
(6 answers)
Closed 8 years ago.
i am trying to connect to TOR through python but it doesnt let me the code is:
def tor_connection():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
socket.socket = socks.socksocket
def main():
tor_connection()
print('Connected to tor')
con = httplib.HTTPConnection('myip.dnsomatic.com/')
con.request('GET', '/')
response = con.getresponse()
print(response.read())
main()
even though its giving me the next error message:
Traceback (most recent call last):
File "C:/Users/anon/PycharmProjects/Scraper/tor.py", line 198, in <module>
main()
File "C:/Users/anon/PycharmProjects/Scraper/tor.py", line 194, in main
con.request('GET', '/')
File "C:\Python27\lib\httplib.py", line 1001, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1035, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 997, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 850, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 812, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 793, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
i am just a beginner could someone help me out please? i have tried it in another laptop but its the same error message
It's not a problem of socks. You need to specify the hostname without the trailing /:
>>> # with /
>>> httplib.HTTPConnection('myip.dnsomatic.com/').request('GET', '/')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 791, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 772, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
>>> # without /
>>> httplib.HTTPConnection('myip.dnsomatic.com').request('GET', '/')
>>>

Categories