#!/usr/bin/python # everclient.py # This is a test Bittorrent client that performs basic operations, used to test the Bittorrent tracker. It's not going to be an important part of the project, but it's much easier simply to write a test client than try and hack an existing one to respond properly. import urllib import bencode import types import time import string # Configuration variables NAME = "EverClient" VERSION = "0.1" SERVER = "hermit" PORT = "6969" # Function sends a HTTP GET request through a url to the tracker, containing data about what torrent the client is peering and variout types status info about the client that the tracker appreciates def send_http_get(): # Construct the dictionary of required data # See http://wiki.theory.org/BitTorrentSpecification for the complete details (this is the most comprehensive page in existence, besides referring directly to the official Bittorrent client's source code). required = { "info_hash":string.lowercase, "peer_id": "evertestclient000000", "port":"6881", "uploaded":"314159", "downloaded":"951413", "left":"1", "compact":"YES!", # I don't think the value matters, it's the presence "event":"started", } optional = { "ip":"127.0.0.1", "numwant":"3", "key": "AWANG", "trackerid":"", } url = "http://" + SERVER + ":" + PORT + "/announce/" # push non-empty optional keys into required dict for key,value in optional.iteritems(): if value == "": pass required[key] = value # Urlencode data so it can be properly passed in HTTP GET data = urllib.urlencode(required) # Construct the url with the data in GET fullurl = url + "?" + data # Send the data with a url request u = urllib.urlopen(fullurl) # Save what the tracker responds with (text/plain document of bencoded data) get = u.read() u.close() # Show tracker response response = bencode.bdecode(get) for k,v in response.iteritems(): if k == "peers": print "Peers\n" for peer in response['peers']: for key,value in peer.iteritems(): print "\t",key,": ",value print else: print k,": ",v def main(): send_http_get() if __name__ == "__main__": main()