request fails on wikipedia (https) - certificate verify failed (_ssl.c:748)

2017-12-11 Thread F Massion
Hi,

I would like to get information from Wikipedia articles and I am testing the 
connection to Wikipedia. 

I am running Python 3.6.2 on Windows 10.

I get certificate errors for all pages with https. 
Any suggestions are welcome!

Here my code:

import requests, bs4
from bs4 import BeautifulSoup
res = requests.get('https://en.wikipedia.org/wiki/Stethoscope')
type(res)
res.status_code == requests.codes.ok
wiki = bs4.BeautifulSoup(res.text)
type(wiki)
len(res.text)
print(res.text[:250])

Here the error message:

Traceback (most recent call last):
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 601, in 
urlopen
chunked=chunked)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 346, in 
_make_request
self._validate_conn(conn)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 850, in 
_validate_conn
conn.connect()
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 329, in 
ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
  File "c:\Program Files (x86)\Python36-32\lib\ssl.py", line 401, in wrap_socket
_context=self, _session=session)
  File "c:\Program Files (x86)\Python36-32\lib\ssl.py", line 808, in __init__
self.do_handshake()
  File "c:\Program Files (x86)\Python36-32\lib\ssl.py", line 1061, in 
do_handshake
self._sslobj.do_handshake()
  File "c:\Program Files (x86)\Python36-32\lib\ssl.py", line 683, in 
do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 
(_ssl.c:748)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 639, in 
urlopen
_stacktrace=sys.exc_info()[2])
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\urllib3\util\retry.py", line 388, in 
increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='en.wikipedia.org', 
port=443): Max retries exceeded with url: /wiki/Stethoscope (Caused by 
SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify 
failed (_ssl.c:748)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "web_1.py", line 3, in 
res = requests.get('https://en.wikipedia.org/wiki/Stethoscope')
  File "c:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", 
line 72, in get
return request('get', url, params=params, **kwargs)
  File "c:\Program Files (x86)\Python36-32\lib\site-packages\requests\api.py", 
line 58, in request
return session.request(method=method, url=url, **kwargs)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
  File "c:\Program Files 
(x86)\Python36-32\lib\site-packages\requests\adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='en.wikipedia.org', 
port=443): Max retries exceeded with url: /wiki/Stethoscope (Caused by 
SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify 
failed (_ssl.c:748)'),))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: request fails on wikipedia (https) - certificate verify failed (_ssl.c:748)

2017-12-12 Thread F Massion
Am Dienstag, 12. Dezember 2017 14:33:42 UTC+1 schrieb Jon Ribbens:
> On 2017-12-11, F Massion  wrote:
> > ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 
> > (_ssl.c:748)
> 
> Try `pip install certifi`

certifi was installed.
If I make the following changes I do not have the error message. I don't 
understand why, but this makes a difference:

#import requests --> import urllib.request

url = 'https://en.wikipedia.org/wiki/Stethoscope'
#res = requests.get(url) --> res = urllib.request.urlopen(url).read() 
-- 
https://mail.python.org/mailman/listinfo/python-list


Variable number of arguments

2018-12-17 Thread F Massion
My script is used by a web interface which gets some parameters from a form.
The parameters are passed on as arguments like this:

(...)
def start(self, _Inputfile ,_Outputfile ,_Stopwordsfile)

As long as the number of arguments is set (i.e. 3), there are no problems 
running the script.

Currently I have e.g. 
ARGV[0] = _Inputfile
ARGV[1] = _Outputfile
ARGV[2] = _Stopwordsfile

Now I want to allow a variable number of arguments, i.e. 1..n input files or 
stopwords lists.

In this case ARGV[0] would become [filename1.txt, filename2.txt,...], but I 
wonder how ARGV[1] would still remain _Outputfile.

I thought of using the argparse module with nargs='+' for a variable number 
arguments but it seems to work only with Python 2.7 and I have 3.6 running.

Any suggestions?
-- 
https://mail.python.org/mailman/listinfo/python-list