File: httpclient.py

package info (click to toggle)
lastfmproxy 1.3b-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 240 kB
  • ctags: 75
  • sloc: python: 1,015; sh: 109; makefile: 9
file content (125 lines) | stat: -rw-r--r-- 3,287 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

import base64
import config
import socket
import string

True = 1
False = 0

class httpclient:

    def __init__(self, host, port = None):
        self.host = host
        self.port = port
        self.path = None
        self.status = None
        self.headers = None
        self.response = None
        self.debug = 0

        if self.host[:7] == "http://":
            self.host = self.host[7:]
            p = self.host.find("/")
            self.path = self.host[p:]
            self.host = self.host[:p]

        if not self.port:
            p = self.host.find(":")
            if p >= 0:
                self.port = int(self.host[p+1:])
                self.host = self.host[:p]
            else:
                self.port = 80

    def readline(self, s):
        res = ""
        while True:
            try:
                c = s.recv(1)
            except:
                break
            res = res + c
            if c == '\n':
                break
            if not c:
                break
        if self.debug:
            print "<<< " + res.replace("\n", "")
        return res

    def post(self, postdata):
        return self.req(None, postdata)

    def req(self, url=None, postdata=None):

        if not url:
            url = self.path

        if not url:
            print "httpclient: No URL?"
            return

        method = "GET"
        if postdata:
            method = "POST"

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if config.useproxy:
            try:
                s.connect((config.proxyhost, config.proxyport))
            except:
                self.response = ""
                return

            req = method + " http://" + self.host + ":" + str(self.port) + url + " HTTP/1.1"
            if self.debug:
                print ">>> " + req
            s.send(req + "\r\n")
            if config.proxyuser != "":
                s.send("Proxy-Authorization: Basic " + base64.b64encode(config.proxyuser + ":" + config.proxypass) + "\r\n")
        else:
            try:
                s.connect((self.host, self.port))
            except:
                self.response = ""
                return
            req = method + " " + url + " HTTP/1.1"
            if self.debug:
                print ">>> " + req
            s.send(req + "\r\n")
        s.send("Host: " + self.host + "\r\n")
        s.send("Connection: close\r\n")

        if postdata:
            s.send("Content-Type: application/x-www-form-urlencoded\r\n")
            s.send("Content-Length: " + str(len(postdata)) + "\r\n")

        s.send("\r\n")

        if postdata:
            if self.debug:
                print ">>> postdata: " + postdata
            s.send(postdata)

        line = self.readline(s)
        self.status = string.rstrip(line)

        self.headers = {}
        while True:
            line = self.readline(s)
            if not line:
                break
            if line == "\r\n":
                break
            tmp = string.split(line, ": ")
            self.headers[tmp[0]] = string.rstrip(tmp[1])

        self.response = ""
        while True:
            line = self.readline(s)
            if not line:
                break
            self.response = self.response + line
        s.close()