博客

  • python捕获ctrl+c中断的两种方法

    1,try…except…

        try:
            while True:
                time.sleep(100)
        except KeyboardInterrupt:
            print('Got signal: SIGINT, shutting down.')
            exit(0)
    

    2, 信号处理

    def handler(signal_received, frame):
        # Handle any cleanup here
        if signal_received == signal.SIGINT:
            print('SIGINT or CTRL-C detected. Exiting gracefully')
            exit(0)
    if __name__ == '__main__':
        signal.signal(signal.SIGINT, handler=handler) # ctlr + c
    

    建议用try…except的方法,通用性强,代码易读性高。

    Views: 61

  • python实现非塞UDP通信的两种方法

    当使用阻塞式UDP socket时无法执行多任务处理,也无法与用户交互,甚至不能响应ctrl+c中断。为了解决这些问题所以要用非阻塞式udp通信。
    1,多线程法

    import socket
    import signal
    import threading
    import time
    
    def handler(signal_received, frame):
        # Handle any cleanup here
        if signal_received == signal.SIGINT:
            print('SIGINT or CTRL-C detected. Exiting gracefully')
            exit(0)
    
    def task(host, port):
        print("udp server is listen on " + str(host) + ':' + str(port))
    
        sock = socket.socket(socket.AF_INET, # Internet
                            socket.SOCK_DGRAM) # UDP
        sock.bind((UDP_IP, UDP_PORT))
    
        while True:
            data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
            print("from %s received message: %s" % (addr, data))
    
    if __name__ == '__main__':
        signal.signal(signal.SIGINT, handler=handler) # ctlr + c
    
        UDP_IP = "0.0.0.0"
        UDP_PORT = 5005
    
        t = threading.Thread(target=task, args=(UDP_IP, UDP_PORT))
        t.daemon = True
        t.start()
    
        while True:
            time.sleep(100)
    

    2, 协程法
    官方文档说不要在应用中使用asyncio,而是应该在框架中用

    import asyncio
    
    class EchoServerProtocol:
        def __init__(self, message, on_con_lost):
            self.message = message
            self.on_con_lost = on_con_lost
            self.transport = None
    
        def connection_made(self, transport):
            self.transport = transport
    
        def datagram_received(self, data, addr):
            print('Received %r from %s' % (data.decode(), addr))
            self.transport.sendto(data, addr)
        def error_received(self, exc):
            print('Error received:', exc)
        def connection_lost(self, exc):
            print("Connection closed")
            self.on_con_lost.set_result(True)
    
    async def main():
        print("Starting UDP server")
    
        # Get a reference to the event loop as we plan to use
        # low-level APIs.
        loop = asyncio.get_running_loop()
        on_con_lost = loop.create_future()
        message = 'Hello World!'
        # One protocol instance will be created to serve all
        # client requests.
        transport, protocol = await loop.create_datagram_endpoint(
            lambda: EchoServerProtocol(message, on_con_lost=on_con_lost),
            local_addr=('127.0.0.1', 9999),
            remote_addr=('127.0.0.1', 5005))
    
        try:
            while True:
                await asyncio.sleep(10)  # Serve for 1 hour.
        finally:
            transport.close()
    
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Got signal: SIGINT, shutting down.')
        exit(0)
    

    Views: 89

  • 2022年世界各国人均GDP(IMF)

    该表默认以国际货币基金组织(IMF)公布的各国家或地区的最新估计值进行排名。如果IMF未能提供某个国家或地区的最新数据,则使用世界银行或联合国提供的最新数据排名。您也可选择列表中的Sort both.gif图标以选择其中一个来源重新排名。

    最左栏的排名数字不包括地区和非IMF成员国(在列表中使用 背景色 标记)。

    注:以下国家和地区无数据:福克兰群岛、直布罗陀、根西行政区、圣座(梵蒂冈)、泽西、纽埃、皮特凯恩群岛、圣赫勒拿、阿森松和特里斯坦-达库尼亚、托克劳和西撒哈拉。

    Views: 46

  • jenkins与gitlab集成实现自动构建

    • 安装插件
      https://github.com/jenkinsci/gitlab-api-plugin #GitLab API Plugin
      https://github.com/jenkinsci/gitlab-branch-source-plugin #GitLab Branch Source Plugin
      https://github.com/jenkinsci/basic-branch-build-strategies-plugin #Basic Branch Build Strategies
      https://plugins.jenkins.io/gitlab-oauth/ #GitLab Authentication
      https://plugins.jenkins.io/permissive-script-security/ #Permissive Script Security,这个只对sandbox中的脚本有效,非sandbox是不行的
    • 配置Gitlab Server
      1. 生成token

        这个token生成后要记好,一会儿要填到jenkins里面
      2. 配置system hook

        密码是在jenkins中设置的
      3. 创建应用

        密码是在jenkins中设置的
        这一步是用于oAuth认证
      4. 配置出站请求

        没有这一步会无法添加webhook
    • 配置Jenkins
      1. 配置Gitlab连接
      2. 配置认证

        然后就可以直接用gitlab帐号来登录jenkins
    • 创建Jenkins流水线
      1. 创建Folder Organization类型的Job
        如图

        这个类型的job会自动扫描指定用户或分组下的gitlab项目
        在owner中输入用户名或分组名(可以指定子组)
        在行为列表下面添加以下新的行为
        discover subgroup projects 支持扫描子组
        discover tags 支持扫描tag
        discover branches 发现分支
        advanced clone behaviours 自定义clone选项,用于clone时支持tags
        然后在build strategies里面添加(用于自动构建tag,默认不会)
        change requests
        regular branches
        tags
    1. 如果只针对单个项目创建流水线可以创建Multibranch Pipeline类型的项目
      其它具体的细节可以参考github上的README.md

    其它有用的插件:
    https://plugins.jenkins.io/pipeline-utility-steps/ Pipeline Utility Steps 常用steps集合
    https://plugins.jenkins.io/inline-pipeline/ Multibranch Pipeline Inline Definition 支持内联Jenkinsfile
    https://plugins.jenkins.io/pipeline-multibranch-defaults/ Multibranch with defaults 支持不同分支和项目共用Jenkinsfile
    https://plugins.jenkins.io/config-file-provider/ Config File Provider 用于配置文件
    存储
    https://plugins.jenkins.io/permissive-script-security/ Permissive Script Security
    注意:
    gitlab连接中配置的用户必须是git项目的成员,否则即使能拉取代码也无法同步jenkins的构建状态到gitlab
    用curl调用gitlab api(测试access token及api是否有效)
    curl –request POST \
    –header “PRIVATE-TOKEN: glpat-1234567890” \
    –url “https://git.hetao.me/api/v4/projects/test%2Ftest/statuses/3212877acf1bb58f05818c95cd981ef243e935d8?state=success”

    Views: 26

  • groovy正则表达式

    正则表达式是在文本中寻找子字符串的一种模式。

    • 定义正则表达式

      Groovy中正则表达式定义是通过 ~'正则表达式'定义的
      def regex =~'chese'

    • 常用正则表达式
    表达式 匹配意义 示例
    ^ 常用正则表达式
    $ 行尾。与字符串的尾部或换行符之前的位置相匹配。不包括换行符本身
    . 匹配除换行符以外的任意一个字符
    \w 字母和数字 [0-9A-Za-z]
    \W 非字母和数字。\w以外的单个字符
    \s 空字符 [\t\n\r\f]
    \S 非空字符
    \d 数字 [0-9]
    \D 非数字
    [] 指定字符范围 如:[0-9]
    * 前面元素至少出现0次
    + 前面元素至少出现1次
    {m} 前面的元素出现m 次
    {m,} 前面的元素至少出现m 次
    {m,n} 前面的元素出现至少m 次,至多n 次
    | 选择,匹配其中的任何一个 (a|b)*指a或b出现任意次
    () 正则表达式的群组化 (ab)* 指ab出现任意次
    [\u4E00-\u9FA5] 汉字
    [^\u4E00-\u9FA5] 非汉字
    • 运算符

      1. 匹配操作符(==~)

      用于检查字符串是否匹配 返回true或false
      如:

      if( "abc" ==~ /abc/)
      {
          println 'true'
      }
          else
      {
          println 'flase'
      }
      
      1. 查找操作符(=~)

      用于在字符串中查找匹配的子字符串

      def string = "id=12345"
      def matcher = string =~ /id=/
      def id = matcher.replaceAll('')
      println id  //运行结果为:12345
      

    转自:https://www.cnblogs.com/sky_online/archive/2010/05/14/1735712.html

    Views: 17

  • nexus开启https

    1. 生成证书
      我是用acme.sh申请免费证书,然后转换为java的keystore格式
      转换为pkcs12格式
      openssl pkcs12 -export -inkey example.key -in cert-chain.txt -out nexus.pkcs12
      生成keystore
      keytool -importkeystore -srckeystore nexus.pkcs12 -srcstoretype PKCS12 -destkeystore keystore.jks
    2. 把证书放在${jetty.etc}/ssl/keystore.jks
    3. 编辑nexus.properties
      data-dir/etc/nexus.properties中添加application-port-ssl=8443
      反注释nexus-args,确保它的值里面包含
      {jetty.etc}/jetty-https.xml
      添加ssl.etc={karaf.data}/etc/ssl(如果有这一行ssl目录会放在data目录下面,如果没有则放在{jetty.etc}里面)
    4. 编辑$install-dir/etc/jetty/jetty-https.xml
      有三处需要填写私钥密码
      指定私钥别名(这个不写也可以)
      jetty
    5. 在仓库管理里面把Base URL修改为域名
    6. 重启nexus

    注意:以上证书和配置文件需要权限正确
    参考:
    https://help.sonatype.com/repomanager3/nexus-repository-administration/capabilities/base-url-capability
    https://www.cnblogs.com/Smbands/p/14430775.html

    附件:
    nexus的docker-compose.yaml配置

    services:
      nexus:
        image: sonatype/nexus3
        restart: always
        hostname: nexus
        ports:
          - "8081:8081/tcp"
          - "8082:8082/tcp"
          - "8083:8083/tcp"
          - "8084:8084/tcp"
          - "8085:8085/tcp"
          - "80:8081/tcp"
          - "443:8443/tcp"
        volumes:
          - ./data:/nexus-data
          - ./deploy:/opt/sonatype/nexus/deploy
          - ./ssl:/opt/sonatype/nexus/etc/ssl
          - ./jetty-https.xml:/opt/sonatype/nexus/etc/jetty/jetty-https.xml
          - /etc/localtime:/etc/localtime
    
    

    Views: 174

  • nexus数据清理

    1. 进入Administration->repository->Cleanup Policies->Create Cleanup Policy
    2. 设置参数
      Name:输入一个名字
      Format:选择All Formats
      Component Age:第一次下载至今的天数
      Component Usage:最后一次下载至今的天数
      条件之间是and的关系,要所有条件同时满足才生效。
    3. 点save保存
    4. 进入Administration->System->Tasks->Create Task
    5. 类型选择Admin – Cleanup repositories using their associated policies
      Enabled:开启
      其它参数根据需要选择,这个Task默认就添加过的
    6. 类型选择Admin – Compact blob store
      Enabled:开启
      其它参数根据需要选择
    7. 设置仓库
      进入仓库的设置
      Cleanup Policies在应用上面仓库的clean策略

    参考:
    https://help.sonatype.com/repomanager3/nexus-repository-administration/repository-management/cleanup-policies

    Views: 59

  • groovy空值判断

    def a = null
    if (!a)
        println('a is empty')
    

    在groovy中null,false,”,0的布尔运算结果都是false,同样可以用!a进行反向判断

    Views: 16

  • powershell打包zip

    Compress-Archive -LiteralPath a.txt,b.txt -DestinationPath a.zip #仅打包文件,忽略路径
    Compress-Archive -Path a -DestinationPath a.zip
    Compress-Archive -Path a\*.txt -DestinationPath a.zip
    

    参考:
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive?view=powershell-7.3

    Views: 15

  • NSIS脚本说明

    page用来描述向导页

    MUI风格支持以下Page

    安装向导页

    MUI_PAGE_WELCOME 欢迎首页
    MUI_PAGE_LICENSE textfile 协议
    MUI_PAGE_COMPONENTS 组件选择
    MUI_PAGE_DIRECTORY 安装目录选择
    MUI_PAGE_STARTMENU pageid variable
    MUI_PAGE_INSTFILES
    MUI_PAGE_FINISH 结束页

    卸载向导页

    MUI_UNPAGE_WELCOME
    MUI_UNPAGE_CONFIRM
    MUI_UNPAGE_LICENSE textfile
    MUI_UNPAGE_COMPONENTS 组件选择
    MUI_UNPAGE_DIRECTORY 安装目录选择
    MUI_UNPAGE_INSTFILES
    MUI_UNPAGE_FINISH

    Section 区段

    用于定义每个组件安装和卸载时执行的脚本
    Section “Installer Section”
    some code
    SectionEnd

    Section “un.Uninstaller Section”
    some code
    SectionEnd

    关于Function

    函数分两种一种是用户自定义函数,需要用Call命令调用,一种是回调函数,是在特定的事件后或前自动调用的,所有的回调函数都以圆点开头,所有可用的回调函数参考这里:https://nsis.sourceforge.io/Docs/Chapter4.html#callbacks

    关于变量

    nsis变量用Var关键字定义,如:

    Var varA;

    然后在脚本中给变量赋值

    Section Install
    
    StrCpy $varA "123"
    
    Section End
    

    预定义常量

    • $PROGRAMFILES, $PROGRAMFILES32, $PROGRAMFILES64

    The program files directory (usually C:\Program Files but detected at runtime). On 64-bit Windows, PROGRAMFILES andPROGRAMFILES32 point to C:\Program Files (x86) while PROGRAMFILES64 points to C:\Program Files. UsePROGRAMFILES64 when installing 64-bit applications.

    • $COMMONFILES, $COMMONFILES32, $COMMONFILES64

    The common files directory. This is a directory for components that are shared across applications (usually C:\Program Files\Common Files but detected at runtime). On 64-bit Windows, COMMONFILES andCOMMONFILES32 point to C:\Program Files (x86)\Common Files while COMMONFILES64 points to C:\Program Files\Common Files. UseCOMMONFILES64 when installing 64-bit applications.

    • $DESKTOP

    The Windows desktop directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    • $EXEDIR

    The directory containing the installer executable (technically this is a variable and you can modify it, but it is probably not a good idea).

    • $EXEFILE

    The base name of the installer executable.

    • $EXEPATH

    The full path of the installer executable.

    • ${NSISDIR}

    A symbol that contains the path where NSIS is installed. Useful if you want to reference resources that are in NSIS directory e.g. Icons, UIs etc.

    When compiled with support for keeping makensis and the data in the same place (the default on Windows), it is in the same place as makensis, on other platforms it is set at compile time (See the INSTALL file for info). In both instances you can modify it at runtime by setting the NSISDIR environment variable. See section 3.1.3 for more info.

    • $WINDIR

    The Windows directory (usually C:\Windows or C:\WinNT but detected at runtime).

    • $SYSDIR

    The Windows system directory (usually C:\Windows\System or C:\WinNT\System32 but detected at runtime).

    • $TEMP

    The temporary directory.

    • $STARTMENU

    The start menu folder (useful for adding start menu items using CreateShortcut). The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    • $SMPROGRAMS

    The start menu programs folder (use this whenever you want $STARTMENU\Programs). The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    • $SMSTARTUP

    The start menu programs / startup folder. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    • $QUICKLAUNCH

    The quick launch folder for IE4 active desktop and above. If quick launch is not available it simply returns the same as $TEMP.

    • $DOCUMENTS

    The documents directory. A typical path for the current user is C:\Users\Foo\My Documents. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is not available on Windows 95 unless Internet Explorer 4 is installed.

    • $SENDTO

    The directory that contains Send To menu shortcut items.

    • $RECENT

    The directory that contains shortcuts to the user’s recently used documents.

    • $FAVORITES

    The directory that contains shortcuts to the user’s favorite websites, documents, etc. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is not available on Windows 95 unless Internet Explorer 4 is installed.

    • $MUSIC

    The user’s music files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is available on Windows ME, XP and above.

    • $PICTURES

    The user’s picture files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is available on Windows 2000, XP, ME and above.

    • $VIDEOS

    The user’s video files directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is available on Windows ME, XP and above.

    • $NETHOOD

    The directory that contains link objects that may exist in the My Network Places/Network Neighborhood folder.

    This constant is not available on Windows 95 unless Internet Explorer 4 with Active Desktop is installed.

    • $FONTS

    The system’s fonts directory.

    • $TEMPLATES

    The document templates directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    • $APPDATA

    The (roaming) application data directory.

    This constant is not available on Windows 95 unless Internet Explorer 4 with Active Desktop is installed.

    • $LOCALAPPDATA

    The local (non-roaming) application data directory. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user. The All Users location is also known as %ProgramData% on Vista and above.

    This constant is available on Windows ME, 2000 and above.

    • $PRINTHOOD

    The directory that contains link objects that may exist in the Printers folder.

    This constant is not available on Windows 95 and Windows 98.

    • $INTERNET_CACHE

    Internet Explorer’s temporary internet files directory.

    This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

    • $COOKIES

    Internet Explorer’s cookies directory.

    This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

    • $HISTORY

    Internet Explorer’s history directory.

    This constant is not available on Windows 95 nor Windows NT 4 unless Internet Explorer 4 with Active Desktop is installed.

    • $PROFILE

    The user’s profile directory. A typical path is C:\Users\Foo.

    This constant is available on Windows 2000 and above.

    • $ADMINTOOLS

    A directory where administrative tools are kept. The context of this constant (All Users or Current user) depends on the SetShellVarContext setting. The default is the current user.

    This constant is available on Windows 2000, ME and above.

    • $RESOURCES

    The resources directory that stores themes and other Windows resources (usually $WINDIR\Resources but detected at runtime).

    This constant is available on Windows XP and above.

    • $RESOURCES_LOCALIZED

    The localized resources directory that stores themes and other Windows resources (usually $WINDIR\Resources\1033 but detected at runtime).

    This constant is available on Windows XP and above.

    • $CDBURN_AREA

    A directory where files awaiting to be burned to CD are stored.

    This constant is available on Windows XP and above.

    • $HWNDPARENT

    HWND of the main window (in decimal).

    • $PLUGINSDIR

    The path to a temporary folder created upon the first usage of a plug-in or a call to InitPluginsDir. This folder is automatically deleted when the installer exits. This makes this folder the ideal folder to hold INI files for InstallOptions, bitmaps for the splash plug-in, or any other file that a plug-in needs to work.

    • $USER.. and $COMMON..

    A handful of constants are available as aliases that are not affected by SetShellVarContext: USERTEMPLATES,USERSTARTMENU, USERSMPROGRAMS,USERDESKTOP, COMMONTEMPLATES,COMMONSTARTMENU, COMMONSMPROGRAMS,COMMONDESKTOP and COMMONPROGRAMDATA.
    INSTDIR

    The installation directory (INSTDIR is modifiable using StrCpy, ReadRegStr, ReadINIStr, etc. – This could be used, for example, in the .onInit function to do a more advanced detection of install location).

    Note that in uninstaller code,INSTDIR contains the directory where the uninstaller lies. It does not necessarily contain the same value it contained in the installer. For example, if you write the uninstaller to WINDIR and the user doesn’t move it,INSTDIR will be WINDIR in the uninstaller. If you write the uninstaller to another location, you should keep the installer’sINSTDIR in the registry or an alternative storing facility and read it in the uninstaller.

    • $OUTDIR

    The current output directory (set implicitly via SetOutPath or explicitly via StrCpy, ReadRegStr, ReadINIStr, etc)

    • $CMDLINE

    The command line of the installer. The format of the command line can be one of the following:

    "full\path to\installer.exe" PARAMETER PARAMETER PARAMETER
    installer.exe PARAMETER PARAMETER PARAMETER
    For parsing out the PARAMETER portion, see GetParameters. If /D= is specified on the command line (to override the install directory) it won't show up in $CMDLINE.
    
    • $LANGUAGE

    The identifier of the language that is currently used. For example, English is 1033. You can only change this variable in .onInit.

    Views: 23