2022年6月2日星期四

Python用wallet文件连接Oracle Autonomous Database

The views expressed in this article are my own and do not necessarily reflect the views of Oracle.

Have you always wanted to connect your Python applications to Oracle Autonomous Transaction Processing (ATP) or to Oracle Autonomous Data Warehouse (ADW)?

This is much easier than you ever thought. In this article I’ll tell you how to do this within a few steps.

First of all, go to the Oracle Cloud Free Tier website and sign up for a cloud account to receive access to Oracle Autonomous Database. Finally, provision an ATP instance. In my case it will be called MSATP.

Let’s start with accessing MSATP with Python. We will make only three steps for this:

  1. Download and configure the Oracle Instant Client
  2. Install the cx_Oracle for Python module
  3. Connect your Python application to the ATP instance
Es wurde kein Alt-Text für dieses Bild angegeben.

Download and configure the Oracle Instant Client

For downloading the Oracle Instant Client go to the download page. Select your architecture and download the Basic Package:

Es wurde kein Alt-Text für dieses Bild angegeben.

Unzip the file into a local directory, for example /home/opc/instantclient_19_9, on a Linux based Oracle Compute Cloud Service instance:

Es wurde kein Alt-Text für dieses Bild angegeben.

In case of no other Oracle software running on your system which could be impacted, add Instant Client to the runtime link path:

Es wurde kein Alt-Text für dieses Bild angegeben.

Finally export the library path in the shell where you run your applications:

Es wurde kein Alt-Text für dieses Bild angegeben.

Let’s continue with installing the cx_Oracle module for Python.

Install the cx_Oracle for Python module

First of all, install Python 3 if you haven’t done it before. In my example I will work with cx_Oracle 8. That’s why Python 3.5 or higher is required:

Es wurde kein Alt-Text für dieses Bild angegeben.

As you have already seen on the architectural diagram above, the cx_Oracle module will use the Oracle Client to communicate over Oracle Net with your ATP instance.

Install the cx_Oracle module from the Python Package Index (PyPI) using pip:

Es wurde kein Alt-Text für dieses Bild angegeben.

That’s it. Let’s start working with cx_Oracle and connect to the ATP instance.

Connect your Python application to the ATP instance

At the beginning you will need to download client security credentials to connect to your ATP instance. For downloading the client credentials (wallet) navigate to the Autonomous Transaction Processing details page of your ATP instance. Click on <<DB Connection>>:

Es wurde kein Alt-Text für dieses Bild angegeben.

Click on << Download Wallet >>:

Es wurde kein Alt-Text für dieses Bild angegeben.

Provide a password for your wallet and download the wallet to your Python environment. Subsequently unzip it, for example to /home/opc/wallet:

Es wurde kein Alt-Text für dieses Bild angegeben.
Es wurde kein Alt-Text für dieses Bild angegeben.

You will need only the following files to work with the cx_Oracle module: tnsnames.ora, sqlnet.ora and cwallet.sso.

You can store the wallet configuration file cwallet.sso separately from the tnsnames.ora and the sqlnet.ora files. Please remember, in any case you should store it securely. It’s important to protect this file and its contents to prevent unauthorized database access!

Edit the sqlnet.ora file with vi and enter the path to the cwallet.sso file:

Es wurde kein Alt-Text für dieses Bild angegeben.

As of now you can start accessing your ATP instance with Python. You can call off cx_Oracle.init_oracle_client() function at the beginning of your code:

Es wurde kein Alt-Text für dieses Bild angegeben.

Alternatively, you can export the environment variable TNS_ADMIN to /home/opc/wallet:

Es wurde kein Alt-Text für dieses Bild angegeben.

Test connection to the ATP instance

The following code snippet will query the products table in the sample schema SH. This read-only schema is being provisioned by default with your ATP instance:

from __future__ import print_function

import cx_Oracle
import sys
import getpass

try:
    cx_Oracle.init_oracle_client(config_dir="/home/opc/wallet")
except Exception as err:
    print(err);
    sys.exit(1);

 
password = getpass.getpass()
connection = cx_Oracle.connect('admin',password,'msatp_low')
 
sql = """
select prod_name from sh.products where rownum<5"""

print("All rows from the above query:")
cursor = connection.cursor()
for result in cursor.execute(sql):
    print(result)

print()

Copy and paste it to a file called querySH.py and run it in your Python environment:

Es wurde kein Alt-Text für dieses Bild angegeben.

This kind of connection is suitable for standalone connections, where your application will manage the user sessions to the ATP instance.

You may consider using cx_Oracle’s connection pooling for more sophisticated, performance critical scenarios and applications frequently connecting users to and disconnecting them from the ATP instance.

Bonus material: Simple Oracle Document Access (SODA)

As stated in the Oracle Documentation Simple Oracle Document Access (SODA) is a set of NoSQL-style APIs that let you create and store collections of documents (in particular JSON) in Oracle Database, retrieve them, and query them, without needing to know Structured Query Language (SQL) or how the documents are stored in the database.

cx_Oracle provides all methods and objects needed for working with SODA in your ATP instance.

The following code snippet will create a new collection in your ATP instance and insert a JSON document into this collection:

from __future__ import print_function
 
import cx_Oracle
import sys
import getpass

try:
    cx_Oracle.init_oracle_client(config_dir="/home/opc/wallet")
except Exception as err:
    print(err);
    sys.exit(1);
 

password = getpass.getpass()

connection = cx_Oracle.connect('admin',password,'msatp_low')

soda = connection.getSodaDatabase()

collection = soda.createCollection("mycollection")

content = {'empid':7782,'empdata': {'firstname': 'Scott', 'lastname': 'Tiger'},"Managerid":7839,"Deptid":10}
 
returnedDoc = collection.insertOneAndGet(content)
key = returnedDoc.key

connection.commit()
 
print('The key of the new SODA document is: ', key)


Copy and paste it to a file called soda.py and run it in your Python environment:

Es wurde kein Alt-Text für dieses Bild angegeben.

You can use the following code snippet to retrieve the JSON document stored in your new collection:

from __future__ import print_function

import cx_Oracle
import sys
import getpass

 
try:
    cx_Oracle.init_oracle_client(config_dir="/home/opc/wallet")
except Exception as err:
    print(err);
    sys.exit(1);

password = getpass.getpass()
key = input("Provide your doc key: ")

connection = cx_Oracle.connect('admin',password,'msatp_low')
soda = connection.getSodaDatabase()
collection = soda.createCollection("mycollection")

content = collection.find().key(key).getOne().getContent()

print(content)

Copy and paste it to a file called getSodaContent.py and run it in your Python environment:

Es wurde kein Alt-Text für dieses Bild angegeben.

That's it! You can now use the power and the efficiency of your Python applications with Oracle Autonomous databases!



I installed the library and when trying to access SQL in jupyter notebook with my credentials the following error appears:

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help


As per documentation accessed on cx_Oracle page.

Step 1: install cx_Oracle

python -m pip install cx_Oracle --upgrade

Step 2: Download and extract Oracle Basic Client

For Windows download and extract Oracle Basic Instatnt client instantclient-basic-windows.x64-19.9.0.0.0dbru.zip file.

Step 3: Inform cx_Oracle module about the Instatnt Client location.

If you stick to documentation and extract them in c:\oracle folder then your script may look like this.

    import cx_Oracle
    cx_Oracle.init_oracle_client(lib_dir=r"C:\oracle\instantclient_19_9")

Now you will be free from the error.

2022年5月31日星期二

ubuntu全新设置用户和key登陆,开启docker

 

    当开启一台全新的ubuntu,登陆后要做的事情。

netstat -aptlun 查看打开的端口

adduser name

usermod -a -G adm name

usermod -a -G sudo name

mkdir ~/.ssh

mv my.pub ~/.ssh/authorized_keys

chmod 700 ~/.ssh/

sudo vi /etc/ssh/sshd_config

sudo visudo

jia  ALL=(ALL) NOPASSWD:ALL

sudo snap install docker

sudo groupadd docker 

sudo gpasswd -a $USER docker

newgrp docker 

cat /etc/group | grep docker

2022年5月3日星期二

Blogger搭建国内可正常访问博客(超详细教程)

 


一、经历

  • 前后折腾博客花了不少时间,之前本来想着弄着玩玩,于是随意买了个便宜的域名:axutongxue.top,几经周折终于搭建出满意的博客后,当机立断新买了axutongxue.com这个域名,准备认真写博了。
  • 中途在Github上用hexo也搭建了一个博客,不过可能是做公众号影响,比较习惯在本地用markdown写好,然后再在线渲染css,网页发布。不太习惯在hexo的发布博文方式,另外感觉如果以后换电脑,Github博客搬迁也是个麻烦,最后还是就想用Blogger了。
  • 写这篇文章,记录一下在Blogger上费下的精力,希望也能对一些想用 Blogger 写博客的人带来帮助。
感谢JoeyLiu大佬在我搭建过程中给了许多的帮助
他的博客:https://blog.iljw.me/
他的公众号:Joeyspace

二、国内访问 Blogger

1 首先准备

请确认自己能够科学上网。
  • 不会的话,我这里写有一篇教程:http://mooc1.chaoxing.com/zt/201723393.html
  • 购买域名。没什么好讲的,各个地方都能买,我图便利,直接在阿里云买的域名。(同一个域名,在腾讯云买还要便宜点,不过因为我上一个域名就在阿里云买的,所以这次顺便就在阿里云买了)

2.设置解析

1.Blogger要求的设置

  • 打开Blogger后台,找到设置—基本,Blogger 会提示你需要设置两个 CNAME 分别解析到ghs.google.com 和 ***.dv.googlehosted.com(每一个人的都不一样)
  • 为了 Blogger 可以在国内访问,我们不能直接按上述的解析。
  • 我们得将第一个 CNAME 解析:记录类型: A 记录,主机记录:WWW,记录值由「ghs.google.com」改成 「国内可访问的 IP」
  • 后面的一个CNAME 解析不变:记录类型:CNAME,主机记录:ai76xxxxxxxxxxx,记录值:gv-j5jxxxxxxxxxx

2. 寻找国内可访问的 IP

  • 可以使用站长之家 Ping 工具:http://ping.chinaz.com/
  • 如下图所示,Ping检测:ghs.google.com
  • 找一个国内可访问的 IP。
  • 注意:香港的IP延迟虽然小,但是不行。

3.域名后台设置解析

打开阿里云控制台—域名—域名列表—解析,照第一步所说的那样样设置,如下图:
至此,域名解析设置完成,当然这样还不够,我们还得修改Blogger自带的博客模板。
备注:如果想要实现不带www访问
添加:记录类型: A 记录,主机记录:@,记录值同为找到的 「国内可访问的 IP」
如下图这样:

这样用 axutongxue.com 也可以访问我的博客了

4.启用https

三、修改 Blogger 的模板代码

我使用的是官方Contempo这套主题模板,简洁,美观,符合我的审美。 折腾过程中,在JoeyLiu大佬帮助下发现Contempo这套模板实际上所需要修改的地方很少,并且JoeyLiu大佬也用的这套模板。
如果你想使用非官方模板,这里有详细的教程:https://www.lawpai.com/2016/10/blogger.html?m=1
提示:在修改代码之前,建议先保存备份一下原来的模板,这是一个好习惯。
Blogger 模板网站地址
  1. http://www.mybloggerthemes.com/
  2. https://btemplates.com/
  3. https://gooyaabitemplates.com/
  4. https://newbloggerthemes.com/

1. 下载Contempo模板xml文件

在弹出的窗口中选择「下载主题背景」

2.修改XML

针对Contempo模板,我们只需要屏蔽Blogger自动加载项即可。
用notepad++、Sublime、Atom等软件打开下载好的XML
我用的是Sublime_Text3,顺便提供一个自用版本的下载地址吧:点击下载
打开XML后,Sublime里面Ctrl+F搜索

1.屏蔽Blogger自动加载项即可

将<head>替换为
&lt;!--<head>--&gt;&lt;head&gt;

将</head>替换为
&lt;/head&gt;&lt;!--</head>--&gt;

将</body>替换为
&lt;!--</body>--&gt;&lt;/body&gt;

2.替换背景图

搜索“url”,大概在第49行,可以找到一个链接,将其替换为你的背景图片地址。
下图是我替换好之后的,我用的是Github图床。
之前一直用的人民网图床,一直不显示背景图 http://bbs1.people.com.cn/postImages/Y0/70/D2/AB/69/1539491146601.jpg,换用Github图床后,即可正常显示背景图(具体什么问题未知)

3.JS问题

到目前为止,博客已经基本上可以不翻墙访问了。但是有一个问题:打开网站后,这两个按钮按不了
因为模板里有这一段代码: <b:template-script async='true' name='indie' version='1.0.0'/>执行它,会加载下述的 js 文件
  • https://resources.blogblog.com/blogblog/data/res/1468123664-indie_compiled.js
记得删除<b:template-script async='true' name='indie' version='1.0.0'/>这行代码,然后翻墙打开上面的网址,右键另存为得到一个.js文件,把它上传到国内空间的对象存储(阿里云,腾讯云、网易云、又拍云、七牛云…都是免费注册)
复制得到js的外链地址,然后在xml文件里面引用此js文件,引用方式见下图(你可以跟着我在相同的位置引用)
<script async='async' src='你的外链地址'/>
至此,xml基本修改完毕,打开blogger后台,上传修改完的主题模板

3.解决缩略图问题

现在打开主页,唯一的问题是缩略图无法显示,原因是Blogger 新版本的语法,其获取文章图片链接时,会将其转换成 https://lh4.googleusercontent.com/proxy/....这种形式,所以不翻墙无法显示。
在这里感谢JoeyLiu大佬提供了解决办法:Blogger 博文首页缩略图解决方法

使用 JavaScript 代码

Blogger 有一个语法可以获取文章的全部内容,我们可以使用 JavaScript 利用正则表达式将文章中的第一张图片链接提取出来。
代码如下:
将 JS 代码放置于 </body> 标签前:
<b:if cond='data:blog.pageType in {"index","searchQuery","searchLabel","archive"}'> <!--如果当前页是首页,搜索页,标签页,那么代码继续执行-->
    <script defer='defer'>
        //<![CDATA[
        var postThumbnails = document.getElementsByClassName("post-thumbnail");
        var postContents = document.getElementsByClassName("post-text");
        for (var i=0;i<postContents.length;i++)
        {
            var postContent = postContents[i].innerText;
            var imgReg = /<img.*?(?:>|\/>)/gi;
            var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
            var imgTags = postContent.match(imgReg);
            imgSrcs = imgTags[0].match(srcReg);
            imgSrc = imgSrcs[1];
            postThumbnails[i].setAttribute('src', imgSrc);
        }
        //]]>
    </script></b:if>
修改模板,搜索data:post.featuredImage,在缩略图处改成下代码:
<b:if cond='data:post.featuredImage'>  <!--判断文章内是否有图片,有则代码继续执行-->
    <div class='snippet-thumbnail'>  <!--创建一个 div 容器,缩略图放置在这里-->
        <img class='post-thumbnail' sizes='(max-width: 800px) 20vw, 128px' src='https://ae01.alicdn.com/kf/HTB1Gb7LUmzqK1RjSZFL5jcn2XXac.gif'/>  <!--预先放置一个加载图片,增强用户体验-->
        <textarea class='post-text' style='display:none;'><data:post.body.escaped/></textarea> <!--这里放置文章全文,图片从中提取,样式设置为不显示-->
    </div></b:if>
此方法优点是无需手动设置缩略图,比较省心
不同模板会有所不同,详情请看大佬原教程的评论区:Blogger 博文首页缩略图解决方法

4.解决头像问题

如果你添加了个人资料小部件
访客不翻墙的话,是无法正常显示头像的

解决办法如下

还是在blogger后台—主题背景—修改HTML,搜索“profile-img”找到这下面一行
把expr:src='dat:authorPhoto.image'修为src='你的头像链接',如下图所示

5.字体问题

现在博客在电脑端可能没问题了,但是在手机端可能还会出现博文、博文标题、博客标题等等不显示的问题,如下图

解决办法

这是字体问题,解决办法也很简单,打开blogger后台—主题背景—自定义
在Blogger 主题背景设计器里面,如下图,把所有的字体设置为Arial,这样就能把上面的问题解决了。

6.评论系统

Blogger 自带的评论自然不翻墙无法正常使用,本站目前评论系统采用Valine
安装教程请看我写的另外一篇教程:为 Blogger 安装 Valine 评论系统

2022年3月27日星期日

Windows 10 HEVC Video Extensions from Device Manufacturer

HEVC Video Extensions & HEIF Image Extensions are required to open Apple iPhone pictures (.heic) on your Windows 10 Computer. 

These extensions used to be free, but the HEVC Video Extensions recently received a pricetag of €0.99. 

There is a way to get it for free through the Microsoft Store by following one of these next steps. 

Copy/paste this link into the browser: 
 
ms-windows-store://pdp/?ProductId=9n4wgh0z6vhq


Microsoft Store - Generation Project

Microsoft Store - Generation Project (v1.2.3) [by @rgadguard & mkuba50] (rg-adguard.net)


https://apps.microsoft.com/store/detail/hevc-video-extensions-from-device-manufacturer/9n4wgh0z6vhq?hl=en-us&gl=us