diff options
author | Mathieu Blondel <mathieu@mblondel.org> | 2007-05-16 14:10:52 +0000 |
---|---|---|
committer | Mathieu BlondelMathieu Blondel <mathieu@mblondel.orgmathieu@mblondel.org> | 2007-05-16 14:10:52 +0000 |
commit | 01bcd6c43f9de2d421835f325825c69de7df457a (patch) | |
tree | e25f63d7a96d1dea45629d173dd638a7c2dbfa38 /src | |
parent | 6857af1197f86797a2482b70c3c0a4f330aa60a2 (diff) | |
download | wikipediafs-01bcd6c43f9de2d421835f325825c69de7df457a.tar.gz |
Fixed proxy and http auth support.
git-svn-id: http://svn.code.sf.net/p/wikipediafs/code/branches/fuse-python-new-api@26 59acd704-e115-0410-a914-e735a229ed7c
Diffstat (limited to 'src')
-rw-r--r-- | src/wikipediafs/article.py | 19 | ||||
-rw-r--r-- | src/wikipediafs/http.py | 57 | ||||
-rw-r--r-- | src/wikipediafs/user.py | 4 |
3 files changed, 55 insertions, 25 deletions
diff --git a/src/wikipediafs/article.py b/src/wikipediafs/article.py index 593a5da..31c110a 100644 --- a/src/wikipediafs/article.py +++ b/src/wikipediafs/article.py @@ -109,10 +109,8 @@ class Article(SGMLParser): conn = ExtendedHTTPConnection(self.host, self.port, self.https) - conn.set_proxy() - if self.httpauth_username and self.httpauth_password: - conn.http_auth(httpauth_username, httpauth_password) + conn.http_auth(self.httpauth_username, self.httpauth_password) conn.add_headers(headers) conn.request(self.edit_page) @@ -158,7 +156,9 @@ class Article(SGMLParser): headers["Cookie"] = self.cookie_str conn = ExtendedHTTPConnection(self.host, self.port, self.https) - conn.set_proxy() + + if self.httpauth_username and self.httpauth_password: + conn.http_auth(self.httpauth_username, self.httpauth_password) conn.add_headers(headers) conn.add_data(params) @@ -187,11 +187,18 @@ if __name__ == "__main__": "basename" : "/mediawiki/index.php", "https" : True } - - # Used username and password if any + if len(sys.argv) == 3: + # Uses username and password if passed user = User(sys.argv[1], sys.argv[2], **params) params["cookie_str"] = user.getCookieString() + elif len(sys.argv) == 5: + # Same as above + http auth + params["httpauth_username"] = sys.argv[3] + params["httpauth_password"] = sys.argv[4] + + user = User(sys.argv[1], sys.argv[2], **params) + params["cookie_str"] = user.getCookieString() art = Article("Test", **params) print art.get() diff --git a/src/wikipediafs/http.py b/src/wikipediafs/http.py index a7124d8..2183a13 100644 --- a/src/wikipediafs/http.py +++ b/src/wikipediafs/http.py @@ -19,7 +19,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os, socket, string, base64 -from httplib import HTTPConnection, HTTPSConnection +import httplib class ExtendedHTTPConnection: """ @@ -33,10 +33,21 @@ class ExtendedHTTPConnection: else: port = int(port) - if https: - self.conn = HTTPSConnection(host, port) + self.https = https + self.port = port + self.host = host + + if os.environ.has_key("http_proxy"): + self.proxy_enabled = True + + self.conn = self.get_proxy_connection() else: - self.conn = HTTPConnection(host, port) + self.proxy_enabled = False + + if https: + self.conn = httplib.HTTPSConnection(host, port) + else: + self.conn = httplib.HTTPConnection(host, port) self.headers = {} self.data = None @@ -48,13 +59,25 @@ class ExtendedHTTPConnection: for k, v in headers.items(): self.add_header(k, v) - def request(self, url): + def request(self, path): if self.data: method = "POST" else: method = "GET" - self.conn.request(method, url, self.data, self.headers) + if self.https: + proto = "https" + else: + proto = "http" + + if self.proxy_enabled: + # the full url is of course needed + url = "%s://%s:%d%s" % (proto, self.host, self.port, path) + else: + url = path + + + return self.conn.request(method, url, self.data, self.headers) def getresponse(self, *args): return self.conn.getresponse(*args) @@ -65,21 +88,23 @@ class ExtendedHTTPConnection: def add_data(self, data): self.data = data - def set_proxy(self): + def get_proxy_connection(self): """ Sets proxy if needed. """ - if os.environ.has_key("http_proxy"): - http_proxy = os.environ["http_proxy"] - http_proxy = http_proxy.replace("http://", "").rstrip("/") - (proxy_host, proxy_port) = http_proxy.split(":") - proxy_port = int(proxy_port) - proxy_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - proxy_sock.connect((proxy_host, proxy_port)) - self.sock = proxy_sock + http_proxy = os.environ["http_proxy"] + http_proxy = http_proxy.replace("http://", "").rstrip("/") + (proxy_host, proxy_port) = http_proxy.split(":") + proxy_port = int(proxy_port) + + if self.https: + return httplib.HTTPConnection(proxy_host, proxy_port) + else: + return httplib.HTTPConnection(proxy_host, proxy_port) + def http_auth(self, username, password): httpbasicauth = "%s:%s" % (username, password) - add_header("Authorization", + self.add_header("Authorization", "Basic %s" % base64.encodestring(httpbasicauth).strip())
\ No newline at end of file diff --git a/src/wikipediafs/user.py b/src/wikipediafs/user.py index 40b45f8..2fae77c 100644 --- a/src/wikipediafs/user.py +++ b/src/wikipediafs/user.py @@ -66,10 +66,8 @@ class User: conn = ExtendedHTTPConnection(self.host, self.port, self.https) - conn.set_proxy() # sets proxy if needed - if self.httpauth_username and self.httpauth_password: - conn.http_auth(httpauth_username, httpauth_password) + conn.http_auth(self.httpauth_username, self.httpauth_password) conn.add_data(params) conn.add_headers(headers) |