Когда я запускаю эту программу и жду пять минут, я получаю указанную ниже ошибку, но я немного не знаком с тем, что здесь происходит. Кто-нибудь знает какие-либо способы исправить указанную выше ошибку, чтобы программа успешно работала? Есть ли что-то конкретное, что я делаю неправильно? Я очень ценю любую помощь.
Код:
import twstock
import time
import requests
def LINE_Notify(token,msg):
headers = {
'Authorization': 'Bearer '+token,
'Content-Type': 'application/x-www-form-urlencoded'
}
payload = {'message':msg}
notify = requests.post('https://notify-api.line.me/api/notify',headers=headers,params=payload)
return notify.status_code
def sendline(mode,realprice,counterLine,token):
print('Current Stock price: ' + str(realprice))
if mode == 1:
message = 'The current stock price is ' + str(realprice) + '. You can sell out now'
else:
message = 'The current stock price is ' + str(realprice) + '. You can buy in now'
code = LINE_Notify(token,message)
if code == 200:
counterLine += 1
print('The message is sent '+ str(counterLine)+' times')
else:
print('Failed to send!')
return counterLine
token ='###'
counterLine = 0
counterError = 0
print('The program starts now')
while True:
realdata = twstock.realtime.get('2317')
if realdata['success']:
realprice = realdata['realtime']['latest_trade_price']
if float(realprice) >= 80:
counterLine = sendline(1,realprice,counterLine,token)
elif float(realprice) <= 60:
counterLine = sendline(2,realprice,counterLine,token)
if counterLine >= 3:
print('Program ends')
break
else:
print('Stock number 2317 data cannot be read: '+ realdata['rtmessage'])
counterError += 1
if counterError >= 3:
print('Program ends')
break
for i in range(300):
time.sleep(1)
Ошибка:
Traceback (most recent call last):
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\contrib\pyopenssl.py", line 488, in wrap_socket
cnx.do_handshake()
File "C:\Python38\lib\site-packages\OpenSSL\SSL.py", line 1934, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "C:\Python38\lib\site-packages\OpenSSL\SSL.py", line 1671, in _raise_ssl_error
_raise_current_error()
File "C:\Python38\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn
conn.connect()
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\connection.py", line 361, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\contrib\pyopenssl.py", line 494, in wrap_socket
raise ssl.SSLError("bad handshake: %r" % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])",)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mis.twse.com.tw', port=443): Max retries exceeded with url: /stock/index.jsp (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/Python/Python Data Science/Ch11 LINE/linestock.py", line 37, in <module>
realdata = twstock.realtime.get('2317')
File "C:\Python38\lib\site-packages\twstock\realtime.py", line 92, in get
data = get_raw(stocks) if not mock else twstock.mock.get(stocks)
File "C:\Python38\lib\site-packages\twstock\realtime.py", line 71, in get_raw
req.get(SESSION_URL, proxies=get_proxies())
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 543, in get
return self.request('GET', url, **kwargs)
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 665, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 665, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 237, in resolve_redirects
resp = self.send(
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Dominic\AppData\Roaming\Python\Python38\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='mis.twse.com.tw', port=443): Max retries exceeded with url: /stock/index.jsp (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))