So I have a website which has a cookie with an integer in it, causing Requests to fail:
import requests
r = requests.get('http://www.c1000.nl/kies-uw-winkel.aspx')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 361, in request
resp = self.send(prep, **send_kwargs)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 464, in send
r = adapter.send(request, **kwargs)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 369, in send
r = self.build_response(request, resp)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 179, in build_response
extract_cookies_to_jar(response.cookies, req, resp)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/cookies.py", line 117, in extract_cookies_to_jar
jar.extract_cookies(res, req)
File "/usr/lib/python2.7/cookielib.py", line 1645, in extract_cookies
self.set_cookie(cookie)
File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/cookies.py", line 270, in set_cookie
if cookie.value.startswith('"') and cookie.value.endswith('"'):
AttributeError: 'int' object has no attribute 'startswith'
Is this a bug in Requests? (i.e. shouldn't it be able to handle integers?)
How can I avoid it? Is there a thing in a get request for avoid cookies?
System info:
Ubuntu 12.04LTS 64bit
Python 2.7.3
Request 2.0.1
This is a known bug, tracked by this GitHub issue. It will be fixed in the next release of Requests.
To handle cookies you should use a session that stores cookies between requests:
s = requests.Session()
r = s.get('http://www.c1000.nl/kies-uw-winkel.aspx',timeout=20)
This should do the trick. Im not sure why your code sample throws this exception though.. just a quick workaround.
Related
import requests
response = requests.get('http://www.httpbin.org/headers')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 412, in send
conn = self.get_connection(request.url, proxies)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 309, in get_connection
proxy_manager = self.proxy_manager_for(proxy)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 182, in proxy_manager_for
manager = self.proxy_manager[proxy] = SOCKSProxyManager(
File "/usr/lib/python3/dist-packages/urllib3/contrib/socks.py", line 192, in __init__
raise ValueError("Unable to determine SOCKS version from %s" % proxy_url)
ValueError: Unable to determine SOCKS version from socks://IP:PORT/
Hell I ran the above two lines it is giving me error while connecting to proxy, so I tried curl
curl -v http://www.httpbin.org/headers
and it works fine, I tried pip install pysocks but it I still get the same error
note:I removed the proxy ip:port from error
I tried the solution on Unable to determine SOCKS version from socks but it gave same error
I have the following code trying to use stashy, to pull and push to Bit Bucket, Well right now I have code that specifically will just display projects/repositories that are given to my account. Very simple.
import stashy
bitbucket = stashy.connect("https://bitbucket.domain.come:port/projects/asdasd/asd/asdadasd/", "username","password")
print(bitbucket.projects.list())
I keep getting this error without any hang or delay, What could be this issue, I am assuming its client sided as again I am not given any delay, making me assume its specifically my machine or my client specifically.
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/Project/ProjectName/test_no_gui.py", line 3, in <module>
print(bitbucket.projects.list())
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\helpers.py", line 78, in list
return list(self.all())
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\helpers.py", line 44, in paginate
response = self._client.get(url, **kw)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\client.py", line 59, in get
return requests.get(self.url(resource), auth=(self._username, self._password), verify=self._verify, **kw)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\api.py", line 55, in get
return request('get', url, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\adapters.py", line 174, in send
timeout=timeout
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 417, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 232, in _get_conn
return conn or self._new_conn()
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 547, in _new_conn
strict=self.strict)
TypeError: __init__() got an unexpected keyword argument 'strict'
Looking at other libs/api's turns out stashy is depreciated? Please correct me if I am wrong. Gathered from other research/looking at best method/practice to get into bit bucket. Looking at atlassian-python-api instead, will report back.
Edit: Ended up using REST API, and Requests. All these libs, happened to be out of date.
I have an application which I'm doing self-signing certificates for using the documentation here.
The application loads that keystore into a jetty config and loads properly and I get a warning of an untrusted certification when browsing to the site.
Then I want to use python to connect to it and I've tried all variations of generating a cert, pem, etc. nothing I do gets me to connect. Simply doing the following -
import requests
requests.get('https://servername:8443', cert=('path\
\to\\cert\\app.cert', '\\\\path\\to\\keystore\\keystore'))
Gives me the following traceback -
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL] PEM lib (_ssl.c:2580)
cert in requests is used for client site certificates, not for the CA store. Use the verify parameter to specify the CA file, e.g.:
requests.get('https://www.example.com', verify='/etc/ssl/certs/ca-certificates.crt')
I have the following snippet of python (2.7.14) which is throwing an exception on the requests.get call. This is running on a FreeBSD server, with an apache front end and all http traffic being redirected to https. Note that I am not overly familiar with python, nor this code. Also the following works fine when running locally on my PC and without https/ssl.
urlstr = getApipath() + 'getData/' + id_data
logging.debug("URL used: %s", urlstr)
try:
resp = requests.get(urlstr)
except Exception as e:
logging.exception("caught exception str: " + str(e))
The urlstr is an api call and is using https. The same api call using curl on the same host's command line returns the expected and valid json.
curl https://my.domain.com/is/api/getData/D_01
The following is the log output including stacktrace when running this (obviously not using real url). Looks like an ssl issue, but the error message string being empty doesn't help. Any help on what the issue could be greatly appreciated.
2018-03-28 10:37:51,530 URL used: https://my.domain.org/is/api/getData/D_01
2018-03-28 10:37:51,531 Starting new HTTPS connection (1): my.domain.org
2018-03-28 10:37:51,540 caught excpetion str: []
Traceback (most recent call last):
File "/var/www/engines/controller/get_data.py", line 25, in getTemplate
resp = requests.get(urlstr)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python2.7/site-packages/urllib3/connectionpool.py", line 850, in _validate_conn
conn.connect()
File "/usr/local/lib/python2.7/site-packages/urllib3/connection.py", line 314, in connect
cert_reqs=resolve_cert_reqs(self.cert_reqs),
File "/usr/local/lib/python2.7/site-packages/urllib3/util/ssl_.py", line 254, in create_urllib3_context
context = SSLContext(ssl_version or ssl.PROTOCOL_SSLv23)
File "/usr/local/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py", line 379, in __init__
self._ctx = OpenSSL.SSL.Context(self.protocol)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 724, in __init__
_openssl_assert(res == 1)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/_util.py", line 67, in openssl_assert
exception_from_error_queue(error)
File "/usr/local/lib/python2.7/site-packages/OpenSSL/_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
Error: []
An upgrade to the python cryptography module fixed the issue. The installed version was 2.1.4. Turns out there was a newer version available - 2.2.2 which after installing cleared the problem.
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.p
y", line 533, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.p
y", line 246, in _get_conn
return conn or self._new_conn()
File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.p
y", line 738, in _new_conn
raise SSLError("Can't connect to HTTPS URL because the SSL "
requests.packages.urllib3.exceptions.SSLError: Can't connect to HTTPS URL becaus
e the SSL module is not available.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "hosts-print.py", line 19, in <module>
host_count_response = requests.get(host_count_url, verify=False)
File "C:\Python34\lib\site-packages\requests\api.py", line 69, in get
return request('get', url, params=params, **kwargs)
File "C:\Python34\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "C:\Python34\lib\site-packages\requests\sessions.py", line 465, in reques
t
resp = self.send(prep, **send_kwargs)
File "C:\Python34\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python34\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module
is not available.
My code used to work fine until I added a certificate to Google chrome. I reinstalled python and the requests library, but it the code ran only once. After that the same error repeats.
You need to point to a .ca file manually (add verify = path_to_ca_file) or you can disable ssl verify = false.
Uninstalling the older versions of Python, and then reinstalling Python as well as the libraries and reinstalling chrome (after deleting cookies, extensions and plug-ins) fixed the issue.