代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
wininet封装模块
作者:
lniwn
/ 发布于
2013/8/20
/
1060
#coding:utf-8 import ctypes from urlparse import urlparse import threading INTERNET_OPEN_TYPE_PRECONFIG = 0 INTERNET_OPEN_TYPE_PROXY = 3 INTERNET_OPEN_TYPE_DIRECT = 1 INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 INTERNET_SERVICE_HTTP = 3 INTERNET_FLAG_RELOAD = 2147483648 HTTP_ADDREQ_FLAG_REPLACE = 2147483648 HTTP_ADDREQ_FLAG_ADD= 536870912 INTERNET_OPEN_TYPE_DIRECT = 1 INTERNET_FLAG_SECURE = 8388608 HTTP_QUERY_STATUS_CODE = 19 wininet = ctypes.windll.LoadLibrary('wininet.dll') class wininetHttp(): def httpRequest(self,url,method="GET",proxy="",username="",password="",headers="",data="",timeout=20): """ 发出一个HTTP请求 参数: url: 完整网址 method: 请求方法,可以是GET或POST proxy: 代理地址 username: 用户名 password: 密码 headers: 协议头 data: 提交数据 timeout: 请求超时值 返回值: 元组(header,html) """ requestThread = _httprequest(url,method,proxy,username,password,headers,data) requestThread.daemon = True #线程随主线程一起退出 requestThread.start() requestThread.join(timeout) ret = requestThread.getResponse() return ret class _httprequest(threading.Thread): response = (0,'') def __init__(self,url,method,proxy,username,password,headers,data): threading.Thread.__init__(self) self.url = url self.url_info = urlparse(url) self.method = method self.proxy = proxy self.username = username self.password = password self.headers = headers self.data = data def run(self): dwAccessType = self.proxy and INTERNET_OPEN_TYPE_PROXY or INTERNET_OPEN_TYPE_DIRECT lpszProxyName = self.proxy and 'http=' + self.proxy or None lpszAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36' hOpen = wininet.InternetOpenA(lpszAgent,dwAccessType,lpszProxyName,None,0) if not hOpen: return nServerPort = (self.url_info.scheme == 'https') and 443 or 80 hConn = wininet.InternetConnectA(hOpen,self.url_info.netloc.lower(),nServerPort,self.username,self.password,INTERNET_SERVICE_HTTP,0,0) if not hConn: wininet.InternetCloseHandle(hOpen) return dwFlags = (self.url_info.scheme == 'https') and INTERNET_FLAG_SECURE or INTERNET_FLAG_RELOAD hRequest = wininet.HttpOpenRequestA(hConn,self.method,self.url_info.path,'HTTP/1.1',None,0,dwFlags,0) if not hRequest: wininet.InternetCloseHandle(hConn) wininet.InternetCloseHandle(hOpen) return if not 'Accept: ' in self.headers: self.headers += 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n' if not 'Referer: ' in self.headers: self.headers += 'Referer: %s\r\n' % self.url if not 'Accept-Language: ' in self.headers: self.headers += 'Accept-Language: zh-CN,zh;q=0.8\r\n' if self.method.upper() == 'GET': wininet.HttpSendRequestA(hRequest, self.headers,len(self.headers),None, 0) elif self.method.upper() == 'POST': if self.data != '': if not 'Content-Type: ' in self.headers: self.headers = self.headers + "Content-Type: application/x-www-form-urlencoded\r\n" self.headers = self.headers + "Content-Length: %s\r\n" % len(self.data) wininet.HttpSendRequestA(hRequest, self.headers,len(self.headers),self.data,len(self.data)) else: wininet.InternetCloseHandle(hRequest) wininet.InternetCloseHandle(hConn) wininet.InternetCloseHandle(hOpen) return data_buf = (ctypes.c_char * 2048)() ret_data = '' x = ctypes.c_int(1) while x.value != 0: wininet.InternetReadFile(hRequest,data_buf,2048,ctypes.byref(x)) buf = (ctypes.c_char * x.value)() ctypes.memmove(buf,data_buf,x.value) ret_data += buf[:x.value] head_buf = ctypes.create_string_buffer(2048) buf_size = ctypes.c_int(2048) wininet.HttpQueryInfoA(hRequest,22,head_buf,ctypes.byref(buf_size),0) wininet.InternetCloseHandle(hRequest) wininet.InternetCloseHandle(hConn) wininet.InternetCloseHandle(hOpen) self.response = (head_buf.value,ret_data) return def getResponse(self): return self.response
试试其它关键字
wininet封装模块
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
lniwn
贡献的其它代码
(
2
)
.
wininet封装模块
.
切割字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3