2017年8月

pyinstaller打包sqlalchemy问题的解决方法

软硬件环境

  • python3
  • sqlalchemy
  • pyintaller 3.2.1

问题描述

最近在用pyinstaller打包一个使用了sqlalchemy库的python3工程中,碰到了打包后的可执行文件报错的问题,如下图所示

pyinstaller_sqlalchemy_error

解决方案

将sqlalchemy.dialects中的mysql引入,然后采用mysql+pymysql的方式打开数据库,代码如下

import pymysql

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects import mysql

username = 'djstava'
passwd = '123456'
server = '192.168.0.100'
port = '3306'
dbname = 'djdb'

cmd_connect = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(username, passwd, server, port, dbname)

engine = create_engine(cmd_connect)

pyinstaller打包命令

pyinstaller -F --clean --distpath shark main.py

其中shark是目标文件夹,main.py是工程入口文件,-F表示生成一个可执行文件.

参考资料

如何修复linux系统中的grub?

Environment

  • ubuntu
  • grub

Intro

The rescue mode is a major GRUB 2 enhancement. If GRUB 2 fails to find a useable grub.cfg and is unable to transfer control to a kernel it will drop to a grub-rescue> prompt. From this prompt the user can investigate problems, make changes, and retry the boot.

Steps

ls

This will display the known devices and partitions. From this information, the user must determine the device and partition on which the system is installed.

set prefix=(hdX,Y)/boot/grub

If incorrect, "no such disk" or "not found" errors will occur later.

set root=(hdX,Y)********

In this command, X is the device/drive, starting with 0. Y is the partition, starting with 1. Example: (hd0,1) is sda1. (hd2,5) is sdc5.

set

Inspect the "prefix=" listing. It should match the root designation in Step 3, in the following format: prefix=(hdX,Y)/boot/grub.

ls /boot

Inspect the contents. The user should see varioius kernels, initrd images and the grub folder. If not, use the ls command to inspect the device and attempt to find these files and folders. If necessary, set another device as root.

insmod /boot/grub/linux.mod

Load (insert module) the linux module. Without this module loaded, the user will receive an "Unknown command linux" message when trying to load the kernel.

linux /vmlinuz root=/dev/sdXY ro

Load the linux kernel, substituting the correct designations for "X" and "Y" (example: sda1). The user will see a message showing the kernel has been loaded.

initrd /initrd.img

Load the initrd image. When pressing ENTER the user may or may not see a message in the terminal.

boot

Attempt to boot using the information entered.

These changes are not permanent. After successfully booting into the system the user should run sudo update-grub and inspect the GRUB 2 configuration file (/boot/grub/grub.cfg).The user may need to reinstall GRUB 2 using sudo grub-install /dev/sdX.

分享python3爬虫及数据分析实战视频教程

软硬件环境

  • python3

分享

来自付费资源,这里分享给需要的朋友。视频基于python3,2016年底录制,由浅入深,结合实战案例,内容比较丰富。有需要的朋友请在评论里留下邮箱地址,我会一一回复,免费提供。

video_01

video_02

python3接收udp视频数据

软硬件环境

  • ubuntu 16.04 64bit
  • python3
  • udp数据源

参考源码

# -*- coding: utf-8 -*-

"""
@author: djstava
@license: MIT Licence 
@contact: djstava@gmail.com
@site: http://www.xugaoxiang.com
@software: PyCharm
@file: main.py
@time: 8/15/17 5:58 PM
"""

import os
import sys
import socket
import struct

# 组播地址
ip = '225.0.0.1'

# 组播端口
port = 9001

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# bind的参数是一个元祖tuple
sock.bind((ip, port))

sock_value = struct.pack('4sl', socket.inet_aton(ip), socket.INADDR_ANY)

# 加入udp组播组
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, sock_value)

fileName = 'test.ts'

# 打开文件
fd = open(fileName, 'ab')

while True:
    data = sock.recv(1024000)
    fd.write(data)

# 关闭文件
fd.flush()
fd.close()

# 释放socket
sock.close()

ubuntu下lvm扩容

软硬件环境

  • ubuntu 16.04 64bit

前言

机器用久了,慢慢地,硬盘空间就不够用了,然后就去买了新硬盘,如何在不动现有系统的情况下无缝的安装新硬盘,达到快速扩容的目的,本文就来解决这个问题。这里有个前提,就是ubuntu系统安装时需要创建LVM。

LVM是什么

LVM,即Logical Volume Manager,逻辑卷管理。LVM可以方便地将一个或多个硬盘在逻辑上集合起来,相当于一个大硬盘。

LVM实战

电脑中原来有一块硬盘,2.7TB,手头上还有另外3块新的SATA硬盘,现在需要将这余下的3块新硬盘添加到电脑中,达到扩容的目的。

lvm_01

把3块新硬盘接到电脑上,查看磁盘情况

lvm_02

处理第一块硬盘/dev/sdb,进入到fdisk命令后,n为创建新分区,回车选择默认值,t为选择类型,一定要选8e,意即LVM

lvm_03

lvm_04

最后执行w命令进行保存

lvm_05

接下来创建PV,Physical Volume,物理卷是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,多了一些与LVM相关的管理参数

lvm_06

查看VG,Volume Group:LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成

lvm_07

这里的longjingdata-vg就是vgdisplay输出的VG Name

lvm_08

lvextend -L +2T /dev/longjingdata-vg/root
resize2fs /dev/longjingdata-vg/root

这样一块新硬盘就添加成功了,其余的2块,依照上面的操作依样画葫芦就行了,这里不再赘述。

最后查看下结果,满足预期。

lvm_09