2012-05-03 13 views
0

私はPython + pygtk + webkitを使用してブラウザを構築しようとしています。今、私は 接続の速度(すなわち、ウェブページをロードする時間またはダウンロード速度...主にhttpを絞りたいと思っています。ネットワーク速度の調整

私は同じように使用できるプロトコルまたはウェブキットの方法はありますか?以下は私のブラウザコードです。事前に感謝!

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%の帯域幅 'を使用したいとしましょう。あなたはread()を実行してから、長い時間だけスリープします。

+0

私は、time.sleep()がサイトの読み込みを遅らせるのに役立ちますが、私が本当に望むのは接続速度を抑えることです。私が実際にやろうとしているのは、サイバーカフェのブラウザを作ることです。 time.sleep()関数では、帯域幅を保存できません。顧客がtime.sleep(10)でビデオを視聴した場合、10秒後に開始される可能性がありますが、10秒後には同じ初期値速度。あなたが私のポイントを得ることを願って。 – nitinsh99

+0

私の答えでは、より小さなデータを読み込むための意味があるので、遅れは数分の1秒になります。 1kBを読み、100msの間スリープします。私が考えることのできる唯一の他の方法は、OS層でそれを処理することです。Linuxではiptablesを使用でき、FreeBSDではipfwまたはpfをQoSで使用できます。しかし、あなたの最初の質問からは、ユーザーレベルの純粋なソリューションがほしいと思っていました。 – chmeee

+0

あなたはもう少しコードを特定してください。私は自分でいくつかのプロトコルを変更しなければならないので、OSのレイヤーはオプションではありません。スピードを抑えるためにいくつかのWebkitメソッドを探したいと思っていました。データを転送するためにいくつかのHTTPプロトコルで作業しています。だから問題はどうすればその特定のメソッドを(もしあれば)変更することができるのですか? – nitinsh99

関連する問題