網路上怎麼找,都找到這種程式碼,但這些改法只是改『區域網路』的部份,並非我真正要的。
improt _winreg
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",0,_winreg.KEY_ALL_ACCESS)
_winreg.SetValueEx(key,"ProxyEnable",0, _winreg.REG_DWORD, 1)
_winreg.SetValueEx(key,"ProxyServer",0, _winreg.REG_SZ, "proxy:port")
真的需要修改的位置應該位於
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
再來得看adsl設定什麼名稱就修改那個檔案
但到這一步,就遇到了難題 登錄檔的 檔式 為 REG_BINARY 二進位檔
不像修改區域網路的部份為 REG_SZ 可以直接設定 實際 IP 數值
winreg.SetValueEx(key,"ProxyServer",0, winreg.REG_SZ, "proxy:port")
網路上也難以找到相關資料,或好用的修改方式、模組
只好認命一點,取出對應的值的替換掉
第一步,一樣先取所在位置的所有值:
key =
_winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Internet
Settings\Connections",0, _winreg.KEY_ALL_ACCESS)
再來查詢 adsl 名稱,取出value
adsl = _winreg.QueryValueEx(key, 'adsl')
返回tuple 值
選第一個
a = adsl[0]
我們所需要修改的部份 為第5個值跟第9個值,還有proxy ip 設定的第17個起,往後算20個位置
每個我們在 IE 裡設定,啟用proxy 或關閉proxy 第5個值都會自動加1
啟用proxy的話 第9個值 為3
瞭解原理後,要修改就不太難了
修改 REG_BINARY 二進位檔,我所能想到的方式就是轉 16進位來替換修改
所以導入 binascii 模組
import binascii
adsl_ascii = binascii.b2a_hex(a)
adsl_ascii 的內容,會是類似以下這樣:
'46000000010000000300000014000000
3231382e3138382e31332e3233373a383
838380000000000000000000000000000
000000000000000000000000000000000
000000000000000000000'
修改方式
adsl_ascii = adsl_ascii[:9] + '1' + adsl_ascii[10:17] + '3' + adsl_ascii[18:]
原本的第5跟第9,換算成16進位變 第10跟第18的位置
替換後轉回 二進位檔
adsl_binary = binascii.a2b_hex(adsl_ascii )
然後再修改註冊表
_winreg.SetValueEx(key, "adsl", 0, _winreg.REG_BINARY, adsl_binary)
這樣 proxy 就啟用了,如要關閉就是用:
adsl_ascii = adsl_ascii[:9] + '0' + adsl_ascii[10:17] + '1' + adsl_ascii[18:]
adsl_binary = binascii.a2b_hex(adsl_ascii )
_winreg.SetValueEx(key, "adsl", 0, _winreg.REG_BINARY, adsl_binary)
但因為proxy 的 ip 還未要設定所以,我們再添加修改ip的位置
# 啟用 Proxy
adsl_ascii = adsl_ascii[:9] + '1' + adsl_ascii[10:17] + '3' + adsl_ascii[18:]
# 設定 Proxy IP
adsl_ascii = adsl_ascii[32] + proxy_ip + i[72:]
但一般proxy IP 格式為 221.130.23.82:80
一樣得先轉成 16進位的格式再來做替換
所以簡單的寫個 轉換函數
def proxy2hex(proxy):
proxy_hex = ''
for i in proxy:
proxy_hex += (hex(ord(i)))[2:]
proxy_hex += '0' * (40 - len(proxy_hex))
return proxy_hex
proxy_ip = proxy2hex("221.130.23.82:80")
'3232312e3133302e32332e38323a383000000000'
adsl_ascii = adsl_ascii[32] + proxy_ip + i[72:]
adsl_binary = binascii.a2b_hex(adsl_ascii )
_winreg.SetValueEx(key, "adsl", 0, _winreg.REG_BINARY, adsl_binary)
這樣就能啟用IE的proxy跟設定ip位置
如不想關掉ie再重開讓設定生效
def refresh(self):
import ctypes
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39
internet_set_option = ctypes.windll.Wininet.InternetSetOptionW
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
如有更好的修改方式,請告知一下,修改REG_BINARY格式蠻麻煩的!!
python學習心得、練習、教學 易於學習、功能強大,語法清晰易讀, 方便後續維護,與其它語言結合性強。 如jython(java)、ironpython(visual studio),等於可拿別的語言優點來做結合,又有個別稱『膠水語言』
2012年8月11日 星期六
LIST的妙用
在彼得.狼的獸窩 看到一遍文章覺得蠻有趣的!
Java & Python 程式碼疊疊樂之二
內容主要比較 python&java寫輸入一分數判定等級ABCDE那種
這種東西常見的寫法大部分都是:
Grade = ["E","D","C","B","A"];
Score = int(raw_input());
print Grade[int((Score-51)/10)];
Java & Python 程式碼疊疊樂之二
內容主要比較 python&java寫輸入一分數判定等級ABCDE那種
這種東西常見的寫法大部分都是:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: gradingRobotOne.py
grade = raw_input("請輸入分數: ") #利用 raw_input 的函式取得鍵盤輸入
score = int(grade) #從 raw_input 取得的字串類型 (String) 資料,轉換為 int 的類型
print "====================="
print "您輸入的分數為:", score
gradeLevel = "未判定!" #預設成績等級為「未判定!」
#利用判斷式把輸入的成績分等級
if score > 100 or score < 0:
print "分數應該介於 0 到 100 之間!"
elif score <= 100 and score >= 90:
gradeLevel = "A"
elif score <= 89 and score >= 80:
gradeLevel = "B"
elif score <= 79 and score >=70:
gradeLevel = "C"
elif score <= 69 and score >=60:
gradeLevel = "D"
else:
gradeLevel = "F"
print "成績評定為:", gradeLevel #印出評定結果那有趣的是,我看最後面有一個回文提供的寫法,蠻有智慧的!Grade = ["E","D","C","B","A"];
Score = int(raw_input());
print Grade[int((Score-51)/10)];
直接利用LIST的特性,判定成績,讓代碼大為減少!!!
2010年1月14日 星期四
102 - Ecological Bin Packing
Background
Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.In this problem you will be solving a bin packing problem that deals with recycling glass.
The Problem
Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.
For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.
在回收玻璃瓶時我們根據其顏色分為三類:棕色、綠色、和透明三種。在問題當中會 有三個回收桶,裡面含有若干個上述三種顏色的瓶子。為了要能徹底做好回收,我們 必須要將一些瓶子從某個回收桶拿出放到另外一個,使得到最後每個桶子裡都只有一 種顏色的瓶子。
我們現在所要做的就只有一個目的,讓移動瓶子的步數盡可能地弄到最少。
我們可以假設這些回收桶都具有無限的容量,所以我們唯一要關心的就只有 如何操作,讓每個桶子裡都只含有一種顏色的瓶子。瓶子的總數量不會超過 2 的 31 次方。
2010年1月13日 星期三
各種程式語言 Hello World 比較…
------------------------------------------------------------
>>> C
#include
main()
{
printf ("Hello,World!\n");
}
------------------------------------------------------------
>>> C
#include
main()
{
printf ("Hello,World!\n");
}
------------------------------------------------------------
2010年1月10日 星期日
2010年1月9日 星期六
python 寫 剪刀、石頭、布
# -*- coding: utf-8 -*-
# PYTHON version: 2.5.4
import random
hand = ['剪刀', '石頭', '布']
pwin = 0
cwin = 0
c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]
while pwin < 3 and cwin < 3:
print '目前成績: 電腦贏了', cwin, '次'
print ' 玩家贏了', pwin, '次\n\n'
c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]
if c == '剪刀' and p == '布' or c == '石頭' and p == '剪刀' or c == '布' and p == '石頭':
print '電腦出:', c, '玩家出:', p
print 'computer win!'
cwin += 1
elif c == '剪刀' and p == '石頭' or c == '石頭' and p == '布' or c == '布' and p == '剪刀':
print '電腦出:', c, '玩家出:', p
print 'player win!'
pwin += 1
elif c == '剪刀' and p == '剪刀' or c == '石頭' and p == '石頭' or c == '布' and p == '布':
print '電腦出:', c, '玩家出:', p
print '平手!!'
if pwin > cwin:
print '恭禧!玩家勝出!'
else:
print '你輸了!!'
# PYTHON version: 2.5.4
import random
hand = ['剪刀', '石頭', '布']
pwin = 0
cwin = 0
c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]
while pwin < 3 and cwin < 3:
print '目前成績: 電腦贏了', cwin, '次'
print ' 玩家贏了', pwin, '次\n\n'
c = random.choice(hand)
pc = input('請選擇-0(剪刀),1(石頭),2(布):')
p = hand[pc]
if c == '剪刀' and p == '布' or c == '石頭' and p == '剪刀' or c == '布' and p == '石頭':
print '電腦出:', c, '玩家出:', p
print 'computer win!'
cwin += 1
elif c == '剪刀' and p == '石頭' or c == '石頭' and p == '布' or c == '布' and p == '剪刀':
print '電腦出:', c, '玩家出:', p
print 'player win!'
pwin += 1
elif c == '剪刀' and p == '剪刀' or c == '石頭' and p == '石頭' or c == '布' and p == '布':
print '電腦出:', c, '玩家出:', p
print '平手!!'
if pwin > cwin:
print '恭禧!玩家勝出!'
else:
print '你輸了!!'
訂閱:
文章 (Atom)

