blueboy

hello world!

git不要每次都输入密码

http://www.jianshu.com/p/81ae6e77ff47

Netgear WNDR3800

Netgear WNDR3800,外置usb,可刷openwrt

可以买来玩玩

VB.NET 多线程玩法

1、使用queue玩

这里需要sleep,不然会被一个线程取完所有的数据。

Public Class Form1

    Dim q As New Queue

    Dim SQL_TB As New Object

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Control.CheckForIllegalCrossThreadCalls = False'允许其他线程操作控件

    End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim ThreadingList As List(Of System.Threading.Thread) = New List(Of System.Threading.Thread)

        Dim data() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13}

        For i As Integer = 0 To UBound(data)

            q.Enqueue(data(i))

        Next

        Console.WriteLine(q.Count)

        For x As Int32 = 0 To 1

            Dim GO As New Threading.Thread(AddressOf SQL_SUB)

            GO.Name = x & "sasasasa"

            GO.IsBackground = True

            GO.Start()

            ThreadingList.Add(GO)

        Next

        For xx As Int32 = 0 To 1

            ThreadingList.Item(xx).Join()

        Next

    End Sub

    Private Sub SQL_SUB()

        'http://blog.sina.com.cn/s/blog_457aa0130100joi6.html

        Dim index As Int32

        Try


            While q.Count > 0

                index = Threading.Interlocked.Increment(SQL_i)

                Dim item As New ListViewItem()

                SyncLock SQL_TB

                    System.Threading.Thread.Sleep(10)

                    Dim elm As Object = q.Dequeue()

                    Console.WriteLine(Thread.CurrentThread.Name)

                    ListBox1.Items.Add(elm)

                    Console.WriteLine(elm)

                End SyncLock

            End While

        Catch ex As Exception

            Thread.CurrentThread.Abort()

        End Try

    End Sub

End Class

2、其他玩法

ii全局变量

Dim ii As Int32

Private Sub SQL_SUB()

        Dim index As Int32

        Try

            While True

                index = Threading.Interlocked.Increment(SQL_i)

                If index > 20 Then Exit While '保证ii<20

                Dim item As New ListViewItem()

                SyncLock SQL_TB

                    ii = ii + 1

                    Console.WriteLine(Thread.CurrentThread.Name)

                    ListBox1.Items.Add(ii)

                    Console.WriteLine(ii)

                End SyncLock

            End While

        Catch ex As Exception

            Thread.CurrentThread.Abort()

        End Try

    End Sub


一些文章

http://www.asp.com.tw/news/knowledge/1/info_tc1.htm

python super 类继承

http://blog.sina.com.cn/s/blog_b369b20d0101k5lz.html

根据以上网址必须基类必须加object,不然报错

class father(object):
    def a(self):
       return '我是爸爸'

class test(father):
    def a(self):
              xx=super(test,self).a()
              return xx+"-----我重写了爸爸"

print test().a()

实际运用,django后台用户只显示用户自己的数据,而不显示别人的数据.

# http://stackoverflow.com/questions/12354099/override-default-queryset-in-django-admin

class RecordAdmin(admin.ModelAdmin):
            list_display = ('id','MyRecords','user_id')
    def get_queryset(self, request):
        qs = super(RecordAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=User.objects.get(username=request.user))



unicode

有时候我们从其它地方接受的字符串经过艰难跋涉,它变了个样。比如收到的是'\u6253\u602a\u8005'而不是u'\u6253\u602a\u8005'。

明明肉眼看起来只需要加个u,但是怎么加呢?

>>s = '\u6253\u602a\u8005'

>>s

'\\u6253\\u602a\\u8005'

>>'u'+s

'u\\u6253\\u602a\\u8005'


这样不行。

而且表面看起来是'\u6253\u602a\u8005',其实内部是'\\u6253\\u602a\\u8005',就是转义了反斜杠,有2个斜杠,而我要的是\u而不是\\u

这时喜欢用黑魔法的可能会想到eval。

>>eval('u"'+s+'"')

u'\u6253\u602a\u8005'

>>print eval('u"'+s+'"')

打怪者


嗯,没错,用eval可以。不过这样实现太蛋疼了。

python就自带了解决方法。就是unicode-escape

>>unicode(s,'unicode-escape')

u'\u6253\u602a\u8005'


另外,还有个跟unicode-escape齐名的东西,叫string-escape。它的作用可以说是“去掉转义,把2个反斜杠变成1个”。看例子。

>>s = 'hello\\nworld'

>>s

'hello\\nworld'

>>s.decode('string-escape')

'hello\nworld'


强制访问google.com

https://www.google.com/ncr

_ssl.c:510: error:14077438:SSL routines:SSL23_GET_

_ssl.c:510: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error



pip install pyopenssl ndg-httpsclient pyasn1

win7 64安装MySQL-python

下载https://www.microsoft.com/en-us/download/details.aspx?id=44266,安装

下载http://dev.mysql.com/downloads/file/?id=378025,安装

复制C:\Program Files\MySQL\MySQL Connector C 6.0.2到C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2

执行pip install MySQL-python

测试:

import MySQLdb

conn=MySQLdb.connect(host='localhost', user='root',passwd='')

cursor=conn.cursor()

cursor.execute("select version()")

print cursor.fetchone()

('5.6.17',)


system权限模拟其他用户session运行程序

右键--属性--配置属性--常规--字符集--使用 Unicode 字符集

右键--属性--配置属性--链接器--输入--添加WtsApi32.lib

注意根据需要更改以下代码DWORD dwsessionsid = 1;//这个是用户session id,system为0,其他用户依次排序


#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <Tlhelp32.h>
#include <tchar.h>
#include <psapi.h>
#include <stdio.h>
#include <STDLIB.H>
#include <tlhelp32.h> 
#include <WtsApi32.h>
#include <Userenv.h>

#pragma comment(lib, "WtsApi32.lib")
#pragma  comment (lib,"psapi")
#pragma comment(lib, "userenv.lib")
#define INVALID_PROCESS_ID -2

BOOL GetSessionUserName(DWORD dwsessionsid,wchar_t username[MAX_PATH])
{
    LPWSTR pbuffer = NULL;
    DWORD dwbuffer = NULL;
    BOOL bres = WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE
        ,dwsessionsid,WTSUserName,&pbuffer,&dwbuffer);

    if (bres == FALSE)
    {
        printf("get sessions name false");
        return FALSE;
    }

    lstrcpy(username,pbuffer);
    WTSFreeMemory(pbuffer);
    return TRUE;
}

BOOL GetSessionsDomain(DWORD dwsessionsid,wchar_t domainname[MAX_PATH])
{
    LPTSTR  pbuffer = NULL;
    DWORD dwbuffer;
    BOOL bres = WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE
        ,dwsessionsid,WTSDomainName,&pbuffer,&dwbuffer);

    if(bres == FALSE)
    {
        printf("get sessions domainname error\r\n");
        return FALSE;
    }
    lstrcpy(domainname,pbuffer);
    WTSFreeMemory(pbuffer);
    return TRUE;
}


BOOL ActiveSessions()
{
    WTS_SESSION_INFO* sessionsinfo = NULL;
    DWORD sessionsinfocount;
    wchar_t domainname[MAX_PATH] = {0};
    wchar_t Username[MAX_PATH] = {0};
    BOOL result = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
        0,1,&sessionsinfo,&sessionsinfocount);

    unsigned int usercount(0);
    int num = 0;
    for (unsigned int i = 0;i<sessionsinfocount;i++)
    {
        if ((sessionsinfo[i].State == WTSActive) || (sessionsinfo[i].State == WTSDisconnected))
        {
            GetSessionUserName(sessionsinfo[i].SessionId,Username);
            GetSessionsDomain(sessionsinfo[i].SessionId,domainname);
            printf("\r\n\tSession Infomation:%d\r\n\t"
                "Session Id:%d\r\n\tSession Username:%S\r\n\t"
                "Session Domain:%S\r\n\t",
                num++,sessionsinfo[i].SessionId,Username,domainname);
        }
    }
    printf("\r\t\n[*]:Current Active Session:%d",num-1);
    WTSFreeMemory(sessionsinfo);
    return TRUE;
}



DWORD FindSessionPid(LPCTSTR lpszProcName)  
{  
    DWORD dwPid = INVALID_PROCESS_ID;
    HANDLE hSnapshot = INVALID_HANDLE_VALUE;
    PROCESSENTRY32 pe32 = {0};
    pe32.dwSize = sizeof(PROCESSENTRY32);

    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (hSnapshot != INVALID_HANDLE_VALUE)
    {
        Process32First(hSnapshot, &pe32);
        do
        {
            if (!lstrcmpi(lpszProcName, pe32.szExeFile))
            {
                dwPid = pe32.th32ProcessID;
                //break;
            }
            pe32.dwSize = sizeof(PROCESSENTRY32);
        } while (Process32Next(hSnapshot, &pe32));
        CloseHandle(hSnapshot);
    }
    return dwPid;
}

BOOL EnableProcessPrivilege(LPCTSTR lpszPrivName, BOOL bEnable = TRUE)
{
    HANDLE hToken; 
    TOKEN_PRIVILEGES tkp;
    BOOL bRet = FALSE;

    bRet = OpenProcessToken(GetCurrentProcess(), 
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);

    if (bRet == FALSE)
    {
        printf("OpenProcessToken error\r\n");
    }

    bRet = LookupPrivilegeValue(NULL, lpszPrivName, &tkp.Privileges[0].Luid);
    //  CHECK_NULL_RET(bRet);
    tkp.PrivilegeCount = 1;  
    tkp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED; 

    bRet = AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    //  CHECK_NULL_RET(bRet);
    bRet = TRUE;

    CloseHandle(hToken);
    return bRet;
}

BOOL LaunchAppIntoDifferentSession(wchar_t Cmd[1024])  
{  
    PROCESS_INFORMATION pi;  
    STARTUPINFO si;  
    BOOL bResult = FALSE;  
    DWORD /*dwSessionId = 1,*/ newsessionsid = 0;  
    HANDLE hPToken, hProcess;  
    HANDLE newToken;
    DWORD dwCreationFlags;  
    //  TOKEN_MANDATORY_LABEL tml;
    PSID pIntegritySid = NULL;
    DWORD dwPid = 0;
    // Log the client on to the local computer.  

    typedef DWORD (WINAPI *__pfnWTSGetActiveConsoleSessionId)();  
    typedef BOOL (WINAPI *__pfnWTSQueryUserToken)( ULONG SessionId, PHANDLE phToken );  

    __pfnWTSGetActiveConsoleSessionId pfnWTSGetActiveConsoleSessionId =  
        (__pfnWTSGetActiveConsoleSessionId)GetProcAddress(LoadLibraryA("kernel32.dll"), "WTSGetActiveConsoleSessionId");  

    __pfnWTSQueryUserToken pfnWTSQueryUserToken =  
        (__pfnWTSQueryUserToken)GetProcAddress(LoadLibraryA("Wtsapi32.dll"), "WTSQueryUserToken");  

    if(pfnWTSGetActiveConsoleSessionId == NULL)  
    {  
        printf("Not found api: WTSGetActiveConsoleSessionId\n");  
        return 0;  
    }  

    if(pfnWTSQueryUserToken == NULL)  
    {  
        printf("Not found api: WTSQueryUserToken\n");  
        return 0;  
    }  

    BOOL bret2 = EnableProcessPrivilege(SE_DEBUG_NAME, TRUE);
    if (bret2 == FALSE)
    {
        printf("EnableProcessPrivilege error:%d\r\n",GetLastError());
    }else
    {
        printf("EnableProcessPrivilege Susccessfuly\r\n");
    }

    dwPid = FindSessionPid(L"explorer.exe");
    printf("%d\n", dwPid);
    if (dwPid == NULL)
    {
        printf("dwpid = %d\r\n",GetLastError());
    }

    ////////////////////////////////////////////////////////////////////////  

    dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT;  
    ZeroMemory(&si, sizeof(STARTUPINFO));  
    ZeroMemory(&pi, sizeof(pi));  

    si.cb= sizeof(STARTUPINFO);  
    si.lpDesktop = L"winsta0\\default";  
    si.wShowWindow = SW_SHOW;
    si.dwFlags=STARTF_USESHOWWINDOW;

    DWORD RetLen = 0;  

    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid);  

    if(!OpenProcessToken(hProcess,  PROCESS_ALL_ACCESS, &hPToken))  
    {  
        char pTemp[121];  
        sprintf_s(pTemp, "Process token open Error: %u\n", GetLastError());  
        printf(pTemp);  
    }  

    if(hPToken == NULL)  
    {  
        printf("Process tokenError: \n");  
    }  

    // Launch the process in the client's logon session.  

    newsessionsid = WTSGetActiveConsoleSessionId();
    if (newsessionsid == 0xFFFFFFFF) {
        printf("No session attached to the console (WTSGetActiveConsoleSessionId failed)\n");
    }
    printf("Got session id [%d]\n", newsessionsid);

    if (!WTSQueryUserToken(newsessionsid, &hPToken)) {
        printf("WTSQueryUserToken failed %d\n", GetLastError());
    }

    if(!DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,&newToken)) {
        printf("DuplicateTokenEx failed %d\n", GetLastError());
    }

    DWORD dwsessionsid = 1;//这个是用户session id,system为0,其他用户一次排序
    SetTokenInformation(newToken,TokenSessionId,&dwsessionsid,sizeof(DWORD));

    RtlZeroMemory(Cmd,0,sizeof(cmd));
    bResult = CreateProcessAsUser(  
        newToken,            // client's access token  
        //L"d:\\test.exe",       // file to execute  
        NULL,
        Cmd,         // command line       
        NULL,              // pointer to process SECURITY_ATTRIBUTES  
        NULL,              // pointer to thread SECURITY_ATTRIBUTES  
        FALSE,             // handles are not inheritable  
        dwCreationFlags ,   // creation flags  
        NULL,              // pointer to new environment block  
        NULL,              // name of current directory  
        &si,               // pointer to STARTUPINFO structure  
        &pi                // receives information about new process  
        );  
    // End impersonation of client.  

    int iResultOfCreateProcessAsUser = GetLastError();  

    if(bResult == FALSE && iResultOfCreateProcessAsUser != 0)  
    {  
        char pTemp[121];  
        sprintf_s(pTemp, "CreateProcessAsUser Error: %u\n", GetLastError());  
        printf(pTemp);  
    }  

    if(pi.hProcess)  
    {  
        CloseHandle(pi.hProcess);  
    }  
    if(pi.hThread)  
    {  
        CloseHandle(pi.hThread);  
    }  

    //Perform All the Close Handles task  

    if(hProcess)  
    {  
        CloseHandle(hProcess);  
    }  

    if(hPToken)  
    {  
        CloseHandle(hPToken);  
    }  

    return bResult;  
}  


int _tmain(int argc,wchar_t* argv[])
{
    ActiveSessions();

    wchar_t* cmd = argv[1];

    LaunchAppIntoDifferentSession(cmd);
    return 0;
}


c++ BYTE、WORD与DWORD类型

在Visual C++ 6.0中,BYTE与WORD,DWORD本质上都是一种无符号整型,它们在WINDEF.H中被定义,定义如下:

typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

       
        也就是说BYTE是无符号的char型(char型本质上也是一种1个字节的整型),WORD是无符号short型,DWORD是无符号long型。
    
       
        而在Visual C++ 6.0中,char型长度为1字节,short型长度为2字节,int和long型长度都为4字节,因此可以认为BYTE与WORD,DWORD定义的变量分别获得了1字节,2字节,4字节内存,正与BYTE与WORD,DWORD的字面意义相符。

    
   即:BYTE=unsigned char(完全等同)
           WORD=unsigned short(完全等同)
           DWORD=unsigned long(完全等同)