首先看一下最终效果,在终端模式下实现类似于聊天的对话框 实现方式如下,通过两个list: iwords和swords分别缓存来自终端输入和socket收到的数据,然后再将数据通过屏幕输出并通过socket接口发送出去,通过四个独立的线程分别完成各自的工作: myinput:获取输入区域内的输入数据,并将数据append到iwords和swords myoutput:检测swords长度并通过swo […]
标签: python
试用python的curses模块创建window
实现效果如下图,在console的上部分显示输入的内容,下部分用于输入: 虽然比较粗糙,但是一般的功能勉强可用了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#!/usr/bin/env python import curses import curses.textpad stdscr = curses.initscr() #通过string来存储输入的内容,每次换行后显示 words = '' def mywin(): global words #创建两个win窗口,stdwin用来输入,stdwin2用来输出 #newwin(行数,列数,y坐标,x坐标) stdwin = curses.newwin(1,curses.COLS,curses.LINES-1,0) stdwin2 = curses.newwin(1000, curses.COLS, 0, 0) lines = 0 while True: stdwin.addstr('>') stdwin.refresh() while True: k = stdwin.getkey() if k != '\n': words += k else: stdwin2.addstr(words + '\n') stdwin2.refresh() lines += int(len(words) / curses.COLS) + 1 if lines >= curses.LINES-2: stdwin2.clear() lines = int(len(words) / curses.COLS) + 1 words='' stdwin.clear() break try: mywin() except Exception: raise Exception finally: curses.endwin() |
已知问题: 1.输入后不能使用backspace或者delete进行删除,方向键等功能键也会被直接识别为键值,而不是执行对应的功能(删除可以用Ctrl+Backspace,但是屏幕不会刷新被删除的内容) 2.输出窗口的行 […]
使用python的configparser模块过滤ini配置文件
python的configparser模块可以用来生成/处理类似下面的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[Section1] Option1 = 1 Option2 = 2 DoNotList = 1 [Section2] Option3 = 3 Option4 = 4 MustDisplay = 1 2 3 [Section3] Option5 = 5 Option6 = 6 |
今天要做的是通过configparser模块读取已有的ini文件并过滤出需要的section项,需求很简单,提取满足如下条件的section名字: 1.包含DoNotList项且值不为0 2.不包含DoNotList项但是包含了MustDisplay项 […]
用Python生成批量命令
有时候遇到要批量修改一些规则的,参数递增(递减)的命令时,手动一条条去改实在是太痛苦了,这时候可以考虑用Python自动生成这些命令,比如生成bat或者sh脚本,然后执行就快捷多了。如下是一个示例,自己在现场遇到这样的需求,当时觉得很迷茫,这个系统没有这样的功能啊!后来偶然间在别人的机器看到了Python图标,于是乎想试试看自己能不能做一个简单的模式出来,简单的goole一下,还真的可行: [cr […]
python:试用qrcode和zbar操作二维码
在一次现场交流中,遇到这么一个问题:如何能方便的从保密单位返回信息!说实话我对这种单位的保密级别存在的意义怀有很大的疑问,它就像GFW只能屏蔽一部分人而不能分割全网,但是相比较两者的影响,前者不牢固的木桶所带来的问题更加严重而已。 闲话少叙,进入正题。像python这么一个开放的平台,每一个新技术出现之后肯定会形成相应的python版本模块,所以今天只是试用一下,至于二维码的概念和详细细节,暂且不 […]
Python Ctypes试用一例
“自己的懒惰,就是别人的进步” 要说难其实一点都不难,关键还是在数据类型转换上,比较恼人,像int,char之类的网上有好多示例,这里记录一下我遇到的麻烦:char*, char**的转换方式。为了更好的理解,我自己做了一个dll,来测试这一部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #define O_SIZE 1024 #define DEBUG 0 #ifdef __cplusplus extern "C" { #endif int __declspec(dllexport) __stdcall pchar_ppchar_t(char* input,char** output) { if(DEBUG)fprintf(stderr,"DEBUG-%s\n",input); *output=(char*)malloc(O_SIZE); memset(*output,'\0',O_SIZE); memcpy(*output,input,strlen(input)); if(DEBUG)fprintf(stderr,"DEBUG-%s\n",*output); return 0; } int __declspec(dllexport) __stdcall pchar_pchar_t(char* input,char* output) { if(DEBUG)fprintf(stderr,"DEBUG-%s\n",input); memcpy(output,input,strlen(input)); if(DEBUG)fprintf(stderr,"DEBUG-%s\n",output); return 0; } #ifdef __cplusplus } #endif |
这是windows版本的,linux上只需要将“__declspec(dllexpo […]