日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

http代理的脚本http_proxy.py

發(fā)布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 http代理的脚本http_proxy.py 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

#!/usr/bin/env python# Copyright (C) 2010, Adam Fourney <afourney@cs.uwaterloo.ca> # # This file is part of Adaptable GIMP # # Adaptable GIMP is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # import sys, subprocess, socket, os, errno, random, hashlib, time, reif sys.platform == 'win32':from threading import Threadimport arrayclass Value:def __init__(self, typecode, arg):self.__arr = array.array(typecode, [arg])def getv(self):return self.__arr[0]def setv(self, val):self.__arr[0] = valvalue = property(getv, setv)else:from multiprocessing import Process, Value# CONSTANTS PROXY_TO = 'www.uwaterloo.ca' HOST = '127.0.0.1' PREFERRED_PORT = 8080 HEARTBEAT_PERIOD = 15 APPLICATION = './gimp-2.6.exe' if sys.platform == 'win32' else './gimp-2.6'#####################################################################def main():thread_q = []shutdown = Value('i', 0)is_online = Value('i', 1) # Set up the sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# Try finding a free port to listen on. # Stop after 10 tries.tries = 10port = PREFERRED_PORTwhile (tries > 0):tries = tries - 1try:s.bind((HOST, port))s.listen(5)print "Listening on port {0}".format(port)breakexcept socket.error, (err, msg):if (tries == 0):raiseelif (err == errno.EADDRINUSE):port = random.randint(1024, 49151)continueelse:raise# Set socket timeouts.settimeout(0.5)# Spawn the heartbeatheartbeat_thread = spawn_best_thread(target=start_heartbeat, args=(shutdown,is_online))heartbeat_thread.start()# Spawn our processapp_thread = spawn_best_thread(target=spawn_app, args=(shutdown,port))app_thread.start()# Handle incoming connectionswhile shutdown.value == 0:# Poll the children and reap zombiesnew_q = []for p in thread_q:if p.is_alive():new_q.append(p)thread_q = new_q# Accept a new connection try:conn, addr = s.accept()except socket.timeout:continueexcept socket.error, err:if (err == errno.EINTR):continueelse:raise# Service the request in a new threadconn.settimeout(None)p = spawn_best_thread(target=service_request, args=(conn,is_online))thread_q.append(p)p.start()s.close()#####################################################################def spawn_best_thread(target, args):if sys.platform == 'win32':return Thread(target=target, args=args)else:return Process(target=target, args=args)#####################################################################def start_heartbeat(shutdown, is_online):sleep_for = 0while shutdown.value == 0:# Sleep for half a second at a time to allow for checking of the# shutdown condition.if (sleep_for > 0):time.sleep(0.5)sleep_for = sleep_for - 0.5continue# Do actual workstart_time = time.clock()previous_status = is_online.valuenew_status = previous_statustry:client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)client.settimeout(HEARTBEAT_PERIOD)client.connect((PROXY_TO, 80))client.sendall("HEAD / HTTP/1.1\r\nHost: {0}\r\nConnection: close\r\n\r\n".format(PROXY_TO))response = ""while 1:data = client.recv(1024)if not data:breakresponse += dataif re.search('^HTTP\/\d\.\d\s+200\s+OK', response):new_status = 1else:new_status = 0except socket.error: new_status = 0except socket.timeout: new_status = 0# Shutdown and close the connection, but report no errorstry: client.shutdown(socket.SHUT_RDWR);except socket.error: passtry: client.close()except socket.error: passif new_status != previous_status:print "Connection status changed. Now {0}".format('Online' if new_status else 'Offline') is_online.value = new_status# Arrange to sleep a littlesleep_for = HEARTBEAT_PERIOD - (time.clock() - start_time)#####################################################################def service_request(conn, is_online):# Read the request, respond and exit.request= ""while 1:data = conn.recv(1024)if not data:breakrequest += data# Requests are terminated by the following sequencepos = request.find("\r\n\r\n")if (pos > -1):data = data[0:pos+4]break response = make_request(request, is_online)conn.sendall(response)try:conn.shutdown(socket.SHUT_RDWR);except socket.error:passconn.close()#####################################################################def make_request(data, is_online):# Split the request into lineslines = data.split("\r\n")if data.endswith("\r\n"):lines.pop()# Hash the first line of the request for use as a keyfirst_line = lines[0];key = hashlib.md5(first_line).hexdigest()# Check for special PROXY messagesif first_line == "PROXY GET STATUS":status_str = "Online" if is_online.value > 0 else "Offline"return "Status: {0}\r\n\r\n".format(status_str)# Exit early if we are offlineif is_online.value == 0:return read_from_cache(key)# Modify the request for proxyingdata = "";for line in lines:if line.startswith('Connection:'):data = data + 'Connection: close' + "\r\n"elif line.startswith('Host:'):data = data + 'Host: {0}'.format(PROXY_TO) + "\r\n"else: data = data + line + "\r\n"# Try to fetch from the server, but fall back on the cache if we're offline try:client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)client.settimeout(HEARTBEAT_PERIOD)client.connect((PROXY_TO, 80))client.sendall(data)except: return read_from_cache(key)# Read the responseresponse = ""while 1:data = client.recv(1024)if not data:breakresponse += dataclient.close()# Cache the response and returnwrite_to_cache(key, response)return response###################################################################### Read a response from the cache. Return 404 if there is a problem. def read_from_cache(key):try:f = open("web_cache/{0}.tmp".format(key), "r")f_data = f.read()f.close()except IOError as (errnum, strerror):if (errnum == errno.ENOENT):response = """HTTP/1.1 404 Not Found\r Content-Type: text/html\r Connection: close\r Content-Length: 78\r \r \r <HTML><HEAD><TITLE>404</TITLE></HEAD><BODY>Page Not Found: 404</BODY></HTML>"""return responseelse:raisereturn f_data ###################################################################### Write a response to the cache. Create a web_cache directory if required. def write_to_cache(key, response):if not os.path.isdir('web_cache'):os.mkdir('web_cache')f = open('web_cache/{0}.tmp'.format(key), 'w') f.write(response) f.close() ###################################################################### Spawn the main process def spawn_app(shutdown, port):os.environ['AGIMP_PROXY_PORT'] = str(port)subprocess.call([APPLICATION])shutdown.value = 1 ######## CALL MAIN ########if __name__ == '__main__':main()

轉(zhuǎn)載于:https://my.oschina.net/u/1385797/blog/174017

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的http代理的脚本http_proxy.py的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲视频综合 | 51成人| 中文字幕av在线播放 | 爽爽淫人 | a视频在线播放 | 特级西西444www大精品视频免费看 | 国产情侣第一页 | 天堂中文在线资源 | 亚洲一区二区三区综合 | 一区二区三区丝袜 | 无码人妻丰满熟妇区五十路百度 | 三级视频在线观看 | 亚洲精品国产成人av在线 | 亚洲女同志亚洲女同女播放 | 艳妇臀荡乳欲伦交换gif | 国产精品久久久久久久蜜臀 | 国产亚洲精品久 | 日韩一级高清 | 新版红楼梦在线高清免费观看 | 天堂中文字幕免费一区 | 色偷偷中文字幕 | 国产精品久久av | 精久久久久 | jizz免费在线观看 | 痴汉电车在线观看 | 狠狠做深爱婷婷久久综合一区 | 亚洲精品字幕 | 亚洲精品乱码久久久久久国产主播 | 人人看人人澡 | 久久福利免费视频 | 打屁股疼的撕心裂肺的视频 | 最新精品在线 | 台湾佬成人中文网222vvv | 国产日产精品一区二区三区四区 | 午夜手机福利 | 九九精品在线观看 | 欧美视频网站 | 成人精品网址 | 国产第页 | 国产精品一区二区无线 | 亚洲日本色 | 潮喷失禁大喷水无码 | 老司机一区二区三区 | 日韩精品在线电影 | 美女视频久久 | av网站网址 | 香蕉综合在线 | 毛片在线免费播放 | 91精品久久久久久综合五月天 | 成人69视频| 不卡视频在线播放 | 在线观看免费黄视频 | 日本美女视频网站 | 一区在线观看视频 | 日本黄色免费网址 | 清清草在线视频 | 欧美日韩中文字幕 | 色姑娘综合网 | 天天狠天天插天天透 | 国产成人+综合亚洲+天堂 | 色片在线播放 | 成人91免费视频 | 国产普通话bbwbbwbbw | 日韩经典一区二区 | 日本美女一区 | 午夜做爰xxxⅹ性高湖视频美国 | 老熟妇高潮一区二区高清视频 | 欧美激情一二三 | 香蕉一区二区三区四区 | 永久免费观看av | 国产精品图片 | 国产黄色网页 | 成人精品在线视频 | 91网页在线观看 | аⅴ天堂中文在线网 | 亚洲二三区 | 又紧又大又爽精品一区二区 | 日韩精品在线观看网站 | 欧美资源在线 | 精品久久久中文字幕 | 在线欧美国产 | 蜜桃久久久久久久 | 亚洲成人午夜电影 | av高清在线观看 | 亚洲国产av一区二区 | 国产视频精品视频 | 在线尤物 | 国产91免费观看 | 国产又黄又粗又猛又爽 | 亚欧在线免费观看 | 免费无码不卡视频在线观看 | 成人av动漫在线 | 欧美熟妇激情一区二区三区 | 午夜视频在线观看一区二区 | 日韩一区二区三区四区在线 | 日韩天堂| 日本猛少妇色xxxxx猛叫 | 制服丝袜在线第一页 | 伊人日韩|