当前位置:
首页 > Python基础教程 >
-
python实战--Http代理服务器
最近打算好好深入研究下python的socket编程, 于是打算学习下,仿写了一下,发现写好还真不容易,中途出现很多问题,果真是看的容易,做起来难啊
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
import socket import thread import urlparse import select BUFLEN = 8192 class Proxy( object ): def __init__( self ,conn,addr): self .source = conn self .request = "" self .headers = {} self .destnation = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self .run() def get_headers( self ): header = '' while True : header + = self .source.recv(BUFLEN) index = header.find( '\n' ) if index > 0 : break #firstLine,self.request=header.split('\r\n',1) firstLine = header[:index] self .request = header[index + 1 :] self .headers[ 'method' ], self .headers[ 'path' ], self .headers[ 'protocol' ] = firstLine.split() def conn_destnation( self ): url = urlparse.urlparse( self .headers[ 'path' ]) hostname = url[ 1 ] port = "80" if hostname.find( ':' ) > 0 : addr,port = hostname.split( ':' ) else : addr = hostname port = int (port) ip = socket.gethostbyname(addr) print ip,port self .destnation.connect((ip,port)) data = "%s %s %s\r\n" % ( self .headers[ 'method' ], self .headers[ 'path' ], self .headers[ 'protocol' ]) self .destnation.send(data + self .request) print data + self .request def renderto( self ): readsocket = [ self .destnation] while True : data = '' (rlist,wlist,elist) = select.select(readsocket,[],[], 3 ) if rlist: data = rlist[ 0 ].recv(BUFLEN) if len (data)> 0 : self .source.send(data) else : break def run( self ): self .get_headers() self .conn_destnation() self .renderto() class Server( object ): def __init__( self ,host,port,handler = Proxy): self .host = host self .port = port self .server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self .server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) self .server.bind((host,port)) self .server.listen( 5 ) self .handler = handler def start( self ): while True : try : conn,addr = self .server.accept() thread.start_new_thread( self .handler,(conn,addr)) except : pass if __name__ = = '__main__' : s = Server( '127.0.0.1' , 8080 ) s.start() import socket import thread import urlparse import select BUFLEN = 8192 class Proxy( object ): def __init__( self ,conn,addr): self .source = conn self .request = "" self .headers = {} self .destnation = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self .run() def get_headers( self ): header = '' while True : header + = self .source.recv(BUFLEN) index = header.find( '\n' ) if index > 0 : break #firstLine,self.request=header.split('\r\n',1) firstLine = header[:index] self .request = header[index + 1 :] self .headers[ 'method' ], self .headers[ 'path' ], self .headers[ 'protocol' ] = firstLine.split() def conn_destnation( self ): url = urlparse.urlparse( self .headers[ 'path' ]) hostname = url[ 1 ] port = "80" if hostname.find( ':' ) > 0 : addr,port = hostname.split( ':' ) else : addr = hostname port = int (port) ip = socket.gethostbyname(addr) print ip,port self .destnation.connect((ip,port)) data = "%s %s %s\r\n" % ( self .headers[ 'method' ], self .headers[ 'path' ], self .headers[ 'protocol' ]) self .destnation.send(data + self .request) print data + self .request def renderto( self ): readsocket = [ self .destnation] while True : data = '' (rlist,wlist,elist) = select.select(readsocket,[],[], 3 ) if rlist: data = rlist[ 0 ].recv(BUFLEN) if len (data)> 0 : self .source.send(data) else : break def run( self ): self .get_headers() self .conn_destnation() self .renderto() class Server( object ): def __init__( self ,host,port,handler = Proxy): self .host = host self .port = port self .server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self .server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) self .server.bind((host,port)) self .server.listen( 5 ) self .handler = handler def start( self ): while True : try : conn,addr = self .server.accept() thread.start_new_thread( self .handler,(conn,addr)) except : pass if __name__ = = '__main__' : s = Server( '127.0.0.1' , 8080 ) s.start() |
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式