2012-05-03 66 views
0

我正在嘗試使用Python + pygtk + webkit構建瀏覽器。現在,我想節流的連接速度(即)加載一個網頁或下載速度的時間..主要是.. ..網絡速度調節

所以,有沒有任何協議或webkit的方法,我可以使用相同的。以下是我的瀏覽器代碼。 Thankx提前!

import webkit 
import gobject 

class Browser: 
    default_site = "http://stackoverflow.com/" 

    def delete_event(self, widget, event, data=None): 
     return False 

    def destroy(self, widget, data=None): 
     gtk.main_quit() 

    def __init__(self): 
     gobject.threads_init() 
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     self.window.set_resizable(True) 
     self.window.connect("delete_event", self.delete_event) 
     self.window.connect("destroy", self.destroy) 

     #webkit.WebView allows us to embed a webkit browser 
     #it takes care of going backwards/fowards/reloading 
     #it even handles flash 
     self.web_view = webkit.WebView() 
     self.web_view.open(self.default_site) 

     toolbar = gtk.Toolbar() 

     #create the back button and connect the action to 
     #allow us to go backwards using webkit 
     self.back_button = gtk.ToolButton(gtk.STOCK_GO_BACK) 
     self.back_button.connect("clicked", self.go_back) 

     #same idea for forward button 
     self.forward_button = gtk.ToolButton(gtk.STOCK_GO_FORWARD) 
     self.forward_button.connect("clicked", self.go_forward) 

     #again for refresh 
     refresh_button = gtk.ToolButton(gtk.STOCK_REFRESH) 
     refresh_button.connect("clicked", self.refresh) 

     #add the buttons to the toolbar 
     toolbar.add(self.back_button) 
     toolbar.add(self.forward_button) 
     toolbar.add(refresh_button) 

     #entry bar for typing in and display URLs, when they type in a site 
     #and hit enter the on_active function is called 
     self.url_bar = gtk.Entry() 
     self.url_bar.connect("activate", self.on_active) 

     #anytime a site is loaded the update_buttons will be called 
     self.web_view.connect("load_committed", self.update_buttons) 

     scroll_window = gtk.ScrolledWindow(None, None) 
     scroll_window.add(self.web_view) 


     vbox = gtk.VBox(False, 0) 
     vbox.pack_start(toolbar, False, True, 0) 
     vbox.pack_start(self.url_bar, False, True, 0) 
     vbox.add(scroll_window) 

     self.window.add(vbox) 
     self.window.show_all() 

    def on_active(self, widge, data=None): 
     '''When the user enters an address in the bar, we check to make 
      sure they added the http://, if not we add it for them. Once 
      the url is correct, we just ask webkit to open that site.''' 
     url = self.url_bar.get_text() 
     try: 
      url.index("://") 
     except: 
      url = "http://"+url 
     self.url_bar.set_text(url) 
     self.web_view.open(url) 

    def go_back(self, widget, data=None): 
     '''Webkit will remember the links and this will allow us to go 
      backwards.''' 
     self.web_view.go_back() 

    def go_forward(self, widget, data=None): 
     '''Webkit will remember the links and this will allow us to go 
      forwards.''' 
     self.web_view.go_forward() 

    def refresh(self, widget, data=None): 
     '''Simple makes webkit reload the current back.''' 
     self.web_view.reload() 

    def update_buttons(self, widget, data=None): 
     '''Gets the current url entry and puts that into the url bar. 
      It then checks to see if we can go back, if we can it makes the 
      back button clickable. Then it does the same for the foward 
      button.''' 
     #self.url_bar.set_text(widget.get_main_frame().get_uri()) 
     url = str(widget.get_main_frame().get_uri()) 
     print url 
     if (url == "http://www.facebook.com/"): 
      self.__go("age_error.html") 
     else:  
      self.url_bar.set_text(widget.get_main_frame().get_uri()) 
      print widget.get_main_frame().get_uri() 
      self.back_button.set_sensitive(self.web_view.can_go_back()) 
      self.forward_button.set_sensitive(self.web_view.can_go_forward()) 

    def main(self): 
     gtk.main() 

if __name__ == "__main__": 
    browser = Browser() 

    browser.main() 

回答

1

如果有一種方法可以使用您自己的發送/接收代碼,您可以將它實現爲read()+ sleep()。例如,假設您希望它使用'50%帶寬',那麼您需要花時間閱讀(),然後再睡一會兒。

+0

我同意你的說法,time.sleep()可以幫助我延遲加載網站,但我真正想要的是扼殺連接速度。我真正想做的是爲網吧做一個瀏覽器,這個網吧可以提供較慢的連接速度,而且價格較低。有了time.sleep()函數,我無法保存帶寬,因爲如果客戶用you.sleep(10)在youtube上觀看視頻,可能會在10秒後開始播放,但在10秒鐘後,它將以相同的初始速度。希望你明白了我的觀點。 – nitinsh99

+0

好吧,在我的回答中,我的意思是讀取較小的數據更多,所以延遲將會在幾秒鐘內完成。讀1kB,睡100ms。我能想到的唯一方法就是在OS層處理它,使用Linux可以使用iptables,在FreeBSD中可以使用ipfw或pf,並使用QoS。但從你最初的問題來看,你似乎想要一個純粹的用戶級解決方案,這將是小睡眠小讀。 – chmeee

+0

可以請你多一點代碼特定.. OS層不是一個選項,因爲我必須自己改變一些協議,我希望找到一些WebKit方法來節制速度..因爲它是一個Web引擎,所以它必須是使用一些http協議傳輸數據..現在問題是我該如何更改該特定方法(如果它存在).. – nitinsh99