pywin32编程实例

这里面技巧太多,
1.使用win32的dll必须使用windll.LoadLibrary
2.获得一个字符串的值c_char_p(addr).value
3.wap拨号
4.使用win32ras 拨号
5. socket.bind

import pycurl 
import StringIO 
import socket
import win32ras
import newhttplib
import sys
from ctypes import *

dll=windll.LoadLibrary("GetAddrDll.dll")
def dialup(name):
    #function requires a dial up connection name to be provided to the function. 
    #This sets up which device / com etc to be used and all of that good stuff. 
    #The phone number and username / password can all be overrided, but it needs 
    #the initial path to the device. 
    #Therefore the code would need to look like the following
    #win32ras.Dial(None, None, ('DialUpConnectionName', '9,,832-XXXX', '', 'accountName', 'password', ''), None)
    params = (name, '*99***1#', '', '', '', '')
    fs, ret = win32ras.Dial(None, None, params, None)
    return fs,ret
def close(fs):
    win32ras.HangUp(fs)
    
    
def connect(host,url,addr):
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    h1 = newhttplib.HTTPConnection(host)
    h1.auto_open = 0
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.bind((addr,10001))
    h1.connect(s)
    h1.request("GET", "/")
    r1 = h1.getresponse()
    print r1.status, r1.reason
    print r1.read()
    s.close()
    
def fetch(name):
    fs, ret= dialup(name)
    addr = dll.GetHostIPByEntryName(c_char_p(name));
    addr=c_char_p(addr).value
    print addr
    for i in range(20):
        #try:
        connect('sn.12530.com',"",addr);
        #except BaseException,e:
        #    pass
    close(fs)
    
def fetch(name,listfile,repeat):
    fs, ret= dialup(name)
    addr = dll.GetHostIPByEntryName(c_char_p(name));
    addr=c_char_p(addr).value
    print addr
    f=open(listfile,'r')
    nums = f.readlines()
    for i in range(repeat):
        for num in nums:
            try:
                print num
                connect('sn.12530.com',num,addr);
            except BaseException,e:
                pass
    close(fs)
    
if __name__ == '__main__':
    fetch(sys.argv[1],'number',2)

你可能感兴趣的:(编程,C++,c,socket,WAP)