def run(self): try: self.downloaded = os.path.getsize(self.filename) except OSError: self.downloaded = 0 self.start_pointer = self.ranges[0] + self.downloaded if self.start_pointer >= self.ranges[1]: print '%s has been over.' % self.filename return types = urllib.splittype(self.url) host, res = urllib.splithost(types[1]) h = HTTP() h.connect(host) h.putrequest('GET', res) h.putheader('Host', host) h.putheader('Range', "bytes=%d-%d" % (self.start_pointer, self.ranges[1])) h.endheaders() response = h._conn.getresponse() data = response.read(self.once_buffer) while data: file_opener = open(self.filename, 'ab+') file_opener.write(data) file_opener.close() self.downloaded += len(data) data = response.read(self.once_buffer) h.close()
def doLookup(cellId, lac, host="www.google.com", port=80): from string import replace from struct import unpack page = "/glm/mmap" http = HTTP(host, port) result = None errorCode = 0 content_type, body = encode_request(cellId, lac) http.putrequest('POST', page) http.putheader('Content-Type', content_type) http.putheader('Content-Length', str(len(body))) http.endheaders() http.send(body) errcode, errmsg, headers = http.getreply() result = http.file.read() # be nice to the web service, will not pause on memoized coordinates time.sleep(0.75) # could need some modification to get the answer: here I just need # to get the 5 first characters if (errcode == 200): (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",result) latitude = latitude / 1000000.0 longitude = longitude / 1000000.0 return latitude, longitude
def fetch_id_from_Google(self, cid, lac, country): latitude = 0 longitude = 0 device = "Motorola C123" country = Translator.Country[country] b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, len(country), country, len(device), device, len('1.3.1'), "1.3.1", len('Web'), "Web", 27, 0, 0, 3, 0, cid, lac, 0, 0, 0, 0) http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(b_string))) http.endheaders() http.send(b_string) code, msg, headers = http.getreply() try: bytes = http.file.read() (a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes) latitude /= 1000000.0 longitude /= 1000000.0 status = CellIDDBStatus.CONFIRMED except: status = CellIDDBStatus.NOT_IN_DB return status, latitude, longitude
def grab_information(cid, lac, mnc=0, mcc=0): country = country_iso(mcc) v("Fetching latitude and longitude...") query = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, len(country), country, len(__DEVICE__), __DEVICE__, len('1.3.1'), "1.3.1", len('Web'), "Web", 27, 0, 0, 3, 0, int(cid), int(lac), 0, 0, 0, 0) http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(query))) http.endheaders() http.send(query) code, msg, headers = http.getreply() result = http.file.read() try: (a, b,errorCode, lat, lon, cov, d, e) = unpack(">hBiiiiih",result) except: a = 0 b = 0 errorCode = 0 lat = 0 lon = 0 cov = 0 d = 0 e = 0 pass v("a=%s, b=%s, errorCode=%s, cov=%s, d=%s, e=%s" % (str(a), str(b), errorCode, str(cov), str(d), str(e))) lat = lat / 1000000.0 lon = lon / 1000000.0 v("Here we go: %s and %s" % (lat, lon)) geo_info = None geo_info_json = None geo_info = grab_geo_info(lat, lon) geo_info = grab_geo_info(-8.064159, -34.896666) print str(geo_info) geo_info_json = json.loads(geo_info) v("Geo Info: %s" % geo_info) v("Geo Info: %s" % str(geo_info_json)) c = Cell(geo_info_json, lat, lon, cov) return c
def fetch_latlong_http(query): http = HTTP("www.google.com", 80) http.putrequest("POST", "/glm/mmap") http.putheader("Content-Type", "application/binary") http.putheader("Content-Length", str(len(query))) http.endheaders() http.send(query) code, msg, headers = http.getreply() result = http.file.read() return result
def fetch_latlong_http(query): http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(query))) http.endheaders() http.send(query) code, msg, headers = http.getreply() result = http.file.read() return result
def fetch_latlong_http(self, query): http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(query))) http.endheaders() http.send(query) code, msg, headers = http.getreply() result = http.file.read() return result
def get_file_size(url): types = urllib.splittype(url) host, res = urllib.splithost(types[1]) h = HTTP() h.connect(host) h.putrequest('HEAD', res) h.putheader('Host', host) h.endheaders() status, reason, headers = h.getreply() return float(headers['Content-Length'])
def action_delete(): vip = sys.argv[3] h = HTTP('home.shaunkruger.com:8087') h.putrequest('DELETE','/module/mod_cluster_admin/vip/'+vip) h.putheader('Authorization','Basic '+base64.standard_b64encode('skruger:testing')) h.endheaders() errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() print f.read() return
def identify(host): h = HTTP(host) h.putrequest('GET', 'http://%s/' % host) h.putheader('Accept', '*/*') h.endheaders() errcode, errmsg, response = h.getreply() h.close() patterns.attempts = response.headers try: str = response['server'] except: str = None return (str, None)
def httpread( url_base, url_tail ): url_content = "" h = HTTP( url_base ) h.putrequest( 'GET', url_tail ) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() url_content = f.read() # Print the raw HTML return url_content
def fetch_id_from_Google(self, cid, lac, country): latitude = 0 longitude = 0 device = "Motorola C123" country = Translator.Country[country] b_string = pack( ">hqh2sh13sh5sh3sBiiihiiiiii", 21, 0, len(country), country, len(device), device, len("1.3.1"), "1.3.1", len("Web"), "Web", 27, 0, 0, 3, 0, cid, lac, 0, 0, 0, 0, ) http = HTTP("www.google.com", 80) http.putrequest("POST", "/glm/mmap") http.putheader("Content-Type", "application/binary") http.putheader("Content-Length", str(len(b_string))) http.endheaders() http.send(b_string) code, msg, headers = http.getreply() try: bytes = http.file.read() (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih", bytes) latitude /= 1000000.0 longitude /= 1000000.0 status = CellIDDBStatus.CONFIRMED except: status = CellIDDBStatus.NOT_IN_DB return status, latitude, longitude
def RetrieveAsFile(self, host, path=''): from httplib import HTTP try: h = HTTP(host) except: self.logprint("Failed to create HTTP connection to %s... is the network available?" % (host)) return None h.putrequest('GET',path) h.putheader('Accept','text/html') h.putheader('Accept','text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode != 200: self.logprint("HTTP error code %d: %s" % (errcode, errmsg)) return None f = h.getfile() return f
def _post_multipart(self, host, selector, fields, files): """ Post fields and files to an http host as multipart/form-data. fields is a sequence of (name, value) elements for regular form fields. files is a sequence of (name, filename, value) elements for data to be uploaded as files Return the server's response page. """ content_type, body = self._encode_multipart_formdata(files) h = HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() logger.debug("File sent with error code " + str(errcode) + "; Message was: " + str(errmsg)) return RequestResult(errcode, errmsg, h.file.read())
def doLookup(cellId, lac, host = "www.google.com", port = 80): page = "/glm/mmap" http = HTTP(host, port) result = None errorCode = 0 content_type, body = encode_request(cellId, lac) http.putrequest('POST', page) http.putheader('Content-Type', content_type) http.putheader('Content-Length', str(len(body))) http.endheaders() http.send(body) errcode, errmsg, headers = http.getreply() result = http.file.read() if (errcode == 200): (a, b,errorCode, latitude, longitude, accuracy, c, d) = unpack(">hBiiiiih",result) latitude = latitude / 1000000.0 longitude = longitude / 1000000.0 return latitude, longitude, accuracy
def RetrieveAsFile(self, host, path=''): from httplib import HTTP try: h = HTTP(host) except: self.logprint( "Failed to create HTTP connection to %s... is the network available?" % (host)) return None h.putrequest('GET', path) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode != 200: self.logprint("HTTP error code %d: %s" % (errcode, errmsg)) return None f = h.getfile() return f
def __makeRequest(self, url, path): from httplib import HTTP http = HTTP(HTTP_PROXY or url.replace("http://", "")) http.putrequest("GET", url + path) http.putheader("Accept", "text/plain") http.endheaders() htcode, errmsg, headers = http.getreply() if htcode not in (200, 403, 404): raise HTTPCaptachaUnexpectedResponse(htcode, errmsg) # endif file = http.getfile() content = file.read() file.close() return htcode, content
def __makeRequest(self, url, path): from httplib import HTTP http = HTTP(HTTP_PROXY or url.replace("http://", "")) http.putrequest("GET", url + path) http.putheader("Accept", "text/plain") http.endheaders() htcode, errmsg, headers = http.getreply() if htcode not in (200, 403, 404): raise HTTPCaptachaUnexpectedResponse(htcode, errmsg) #endif file = http.getfile() content = file.read() file.close() return htcode, content
def post_multipart(host, port, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = HTTPConnection(host, port) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() if _python2: h.send(body) else: h.send(body.encode('utf-8')) if _python2: errcode, errmsg, headers = h.getreply() if errcode != 200: raise HTTPException("%s: %s" % (errcode, errmsg)) return h.file.read() else: res = h.getresponse() if res.status != 200: raise HTTPException("%s: %s" % (res.status, res.reason)) return res.read()
def server_lookup(self, addr): req = HTTP("deinadmin.de") req.putrequest("GET", "/projects/pharmcheck/pharmcheck.php?name=" + addr) req.putheader("Host", "deinadmin.de") req.putheader("User-Agent", "pharmcheck v0.3 2005-08-14") req.endheaders() ec, em, h = req.getreply() if (ec != 200): return [-2, ec, em] else: fd=req.getfile() lines=[] line=fd.readline() while line: lines.append(line[:-1]) # \n abschneiden. windows-kompatibel? line=fd.readline() fd.close() return [1, lines]
def action_list(): h = HTTP(surrogate_conf.get('hostname')) h.putrequest('GET','/module/mod_cluster_admin/vip') userpass = "******" % (surrogate_conf.get('username'),surrogate_conf.get('password')) h.putheader('Authorization','Basic '+base64.standard_b64encode(userpass)) h.endheaders() errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() data= f.read() vip = json.loads(data) # Convert json string to python array object formatstr = "%-35s %-8s %s" # reusable format string for header and data output lines print formatstr % ("Address", "Status","Nodes") print "===============================================================================" for v in vip["items"]: print formatstr % (v["address"], v["status"],v["nodes"]) elif errcode == 401: print "Authentication error." else: print "HTTP %s: %s" % (errcode,errmsg) return
def get(host,port,url): concaturl = host+url print "Checking Host:",concaturl h = HTTP(host, port) h.putrequest('GET', url) h.putheader('Host', host) h.putheader('User-agent', 'python-httplib') h.endheaders() time.sleep(1) (returncode, returnmsg, headers) = h.getreply() if returncode != 200: print returncode,returnmsg return returncode else: f = h.getfile() # return f.read() #returns content of page for further parsing print returncode, returnmsg return returncode
def download_web_page(domain, url): try: h = HTTP(domain) h.putrequest("GET", url) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() except: return (None) try: errcode, errmsg, headers = h.getreply() except: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None if errcode != 200: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None results = h.getfile().read() return (results)
def download_web_page(domain, url): try: h = HTTP(domain) h.putrequest("GET", url) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() except: return(None) print "Placed request, downloading web page..." try: errcode, errmsg, headers = h.getreply() except: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None if errcode != 200: sys.stderr.write("Error in receiving response from " + domain + \ '\n') return None results = h.getfile().read() return(results)
def send_request(self, request, response): h = HTTP(urlparse(request.uri)[1]) #TODO: split out port, userinfo h.putrequest(request.method, request.uri) for field_name, field_value in request.representation.headers.items(): if field_name == "Content-Length": continue h.putheader(field_name, field_value.string) if request.representation.has_body: h.putheader('Content-Length', str(len(request.representation.body))) #FIXME: hmm. accesses body. options? h.putheader('User-Agent', self.user_agent) # FIXME: use header dict, don't override h.endheaders() if request.representation.has_body: h.send(request.representation.body) #FIXME: use iterator? status_code, status_phrase, headers = h.getreply() response_type = status.lookup.get(status_code, None) if response_type is not None: response.__class__ = response_type response.status_code = status_code response.status_phrase = status_phrase response.representation.headers.parseMessage(headers) #FIXME: split entity and message hdrs response.representation._body_iter = h.getfile() #FIXME: iterator, shouldn't have to do this _ ... if not isinstance(response, status.Successful): raise response
from httplib import HTTP req = HTTP("www.example.com") req.putrequest("GET", "/index.html") req.putheader("Accept", "text/html") req.putheader("User-Agent", "MyPythonScript") req.endheaders() ec, em, h = req.getreply() print(ec, em) # 200 OK fd = req.getfile() textlines = fd.read() fd.close()
from httplib import HTTP req = HTTP("www.zoo-berlin.de") req.putrequest("GET", "/?Knut") req.putheader("Accept", "text/html") req.putheader("User-Agent", "Python26") req.endheaders() ec, em, h = req.getreply() print ec, em fd = req.getfile() textlines = fd.read() print textlines fd.close()
from httplib import HTTP request = HTTP('apis.skplanetx.com') url = '/tmap/traffic?centerLon=126.976937¢erLat=37.575432&version=1&trafficType=AROUND&reqCoordType=WGS84GEO&zoomLevel=14&radius=1&resCoordType=WGS84GEO' request.putrequest('GET', url) request.putheader('x-skpop-userId', 'game2k') request.putheader('Accept-Language', 'ko_KR') request.putheader('Accept', 'application/json') request.putheader('access_token', '') #request.putheader('appKey','') request.putheader('Host', 'apis.skplanetx.com') request.endheaders() errorCode, errorMessage, headers = request.getreply() print errorCode, errorMessage fobj = request.getfile() html = fobj.read() fobj.close() print html #"GET /
def __call__(self,*args,**kw): method=self.method if method=='PUT' and len(args)==1 and not kw: query=[args[0]] args=() else: query=[] for i in range(len(args)): try: k=self.args[i] if kw.has_key(k): raise TypeError, 'Keyword arg redefined' kw[k]=args[i] except IndexError: raise TypeError, 'Too many arguments' headers={} for k, v in self.headers.items(): headers[translate(k,dashtrans)]=v method=self.method if headers.has_key('Content-Type'): content_type=headers['Content-Type'] if content_type=='multipart/form-data': return self._mp_call(kw) else: content_type=None if not method or method=='POST': for v in kw.values(): if hasattr(v,'read'): return self._mp_call(kw) can_marshal=type2marshal.has_key for k,v in kw.items(): t=type(v) if can_marshal(t): q=type2marshal[t](k,v) else: q='%s=%s' % (k,quote(v)) query.append(q) url=self.rurl if query: query='&'.join(query) method=method or 'POST' if method == 'PUT': headers['Content-Length']=str(len(query)) if method != 'POST': url="%s?%s" % (url,query) query='' elif not content_type: headers['Content-Type']='application/x-www-form-urlencoded' headers['Content-Length']=str(len(query)) else: method=method or 'GET' if (self.username and self.password and not headers.has_key('Authorization')): headers['Authorization']=( "Basic %s" % encodestring('%s:%s' % (self.username,self.password)).replace( '\012','') ) try: h=HTTP(self.host, self.port) h.putrequest(method, self.rurl) for hn,hv in headers.items(): h.putheader(translate(hn,dashtrans),hv) h.endheaders() if query: h.send(query) ec,em,headers=h.getreply() response =h.getfile().read() except: raise NotAvailable, RemoteException( NotAvailable,sys.exc_info()[1],self.url,query) if (ec - (ec % 100)) == 200: return (headers,response) self.handleError(query, ec, em, headers, response)
#c = hex(divmod(mnc,100)[1])[2:].zfill(8) + hex(divmod(mnc,100)[0])[2:].zfill(8) #string = (a + b + c + "FFFFFFFF00000000").decode("hex") #r = urllib.urlopen("http://www.google.com/glm/mmap",string).read().encode("hex") #if len(r) > 14: # lon, lat = float(int(r[14:22], 16)) / 1000000, float(int(r[22:30], 16)) / 1000000 cid, lac, mnc = [int(cid, 16), int(lac, 16), int(mnc)] country = 'fr' device = 'Nokia N95 8Gb' b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, len(country), country, len(device), device, len('1.3.1'), "1.3.1", len('Web'), "Web", 27, 0, 0, 3, 0, cid, lac, 0, 0, 0, 0) http = HTTP('www.google.com', 80) http.putrequest('POST', '/glm/mmap') http.putheader('Content-Type', 'application/binary') http.putheader('Content-Length', str(len(b_string))) http.endheaders() http.send(b_string) code, msg, headers = http.getreply() bytes = http.file.read() (a, b, errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih", bytes) lat = latitude / 1000000.0 lon = longitude / 1000000.0 print "[+] Coordinates : %f:%f" % (lat, lon) if not BTS.if_already_mapped(lat, lon): try: bts = BTS.create(op=op,
# # 1997.02.09 Jim Tittsler [email protected] # import os import regex from httplib import HTTP hostname = "www.dtinet.or.jp" url = "/~tpc/address.htm" print "TPC News Address Updater 1997.02.10 23:20JST [email protected]" print "Making connection to http://%s%s..." % (hostname, url) h = HTTP(hostname) h.putrequest('GET', url) h.putheader('Accept', 'text/html') h.putheader('Accept', 'text/plain') h.endheaders() errcode, errmsg, headers = h.getreply() if errcode != 200: print "Unable to connect" print "Reading current address..." f = h.getfile() cahtml = f.readlines() f.close() print " ", cahtml[1] windir = "C:\WINDOWS" if os.environ.has_key("windir"): windir = os.environ["windir"]
print "Content-Type: text/html" print print "<html><body><form method='GET' action='inspector.py'>URL to inspect: <input name='url'></form></body></html>" else: url_re = compile("http://(?P<host>[^/]*)(?P<path>/.*)?") match = url_re.match(form["url"].value) if not match: print "Content-Type: text/html" print print "<html><body>Malformed URL.</body></html>" else: host = match.group("host") path = match.group("path") http = HTTP(host) http.putrequest("GET", path) http.putheader("Host", host) http.endheaders() errcode, errmsg, headers = http.getreply() if errcode != 200: print "Content-Type: text/html" print print "<html><body>Failed to load URL: %d</body></html>" % errcode else: if headers.has_key("Content-Type"): print "Content-Type: " + headers["Content-Type"] else: print "Content-Type: text/plain" print
def __call__(self, *args, **kw): method = self.method if method == 'PUT' and len(args) == 1 and not kw: query = [args[0]] args = () else: query = [] for i in range(len(args)): try: k = self.args[i] if kw.has_key(k): raise TypeError, 'Keyword arg redefined' kw[k] = args[i] except IndexError: raise TypeError, 'Too many arguments' headers = {} for k, v in self.headers.items(): headers[translate(k, dashtrans)] = v method = self.method if headers.has_key('Content-Type'): content_type = headers['Content-Type'] if content_type == 'multipart/form-data': return self._mp_call(kw) else: content_type = None if not method or method == 'POST': for v in kw.values(): if hasattr(v, 'read'): return self._mp_call(kw) can_marshal = type2marshal.has_key for k, v in kw.items(): t = type(v) if can_marshal(t): q = type2marshal[t](k, v) else: q = '%s=%s' % (k, quote(v)) query.append(q) url = self.rurl if query: query = '&'.join(query) method = method or 'POST' if method == 'PUT': headers['Content-Length'] = str(len(query)) if method != 'POST': url = "%s?%s" % (url, query) query = '' elif not content_type: headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Length'] = str(len(query)) else: method = method or 'GET' if (self.username and self.password and not headers.has_key('Authorization')): headers['Authorization'] = ("Basic %s" % encodestring( '%s:%s' % (self.username, self.password)).replace('\012', '')) try: h = HTTP(self.host, self.port) h.putrequest(method, self.rurl) for hn, hv in headers.items(): h.putheader(translate(hn, dashtrans), hv) h.endheaders() if query: h.send(query) ec, em, headers = h.getreply() response = h.getfile().read() except: raise NotAvailable, RemoteException(NotAvailable, sys.exc_info()[1], self.url, query) if (ec - (ec % 100)) == 200: return (headers, response) self.handleError(query, ec, em, headers, response)
def http_import(self, url, method='GET', auth=None, parse_qs=0, timeout=5): HTTP_PREFIX = 'http://' # Get Query-String. qs = '' i = url.find('?') if i > 0: qs = url[i + 1:] url = url[:i] # Get Host. host = '' servername = url if servername.startswith(HTTP_PREFIX): servername = servername[len(HTTP_PREFIX):] if servername.find('/') > 0: servername = servername[:servername.find('/')] useproxy = True noproxy = ['localhost', '127.0.0.1'] + filter( lambda x: len(x) > 0, map(lambda x: x.strip(), self.getConfProperty('HTTP.noproxy', '').split(','))) for noproxyurl in noproxy: if fnmatch.fnmatch(servername, noproxyurl): useproxy = False break if useproxy: host = self.getConfProperty('HTTP.proxy', host) if len(host) == 0: # Remove HTTP-Prefix. if url.startswith(HTTP_PREFIX): url = url[len(HTTP_PREFIX):] i = url.find('/') if i > 0: host = url[:i] url = url[i:] else: host = url url = '/' # Get Port. i = host.find(':', max(0, host.find('@'))) port = 80 if i > 0: port = int(host[i + 1:]) host = host[:i] # Open HTTP connection. writeLog( self, "[http_import.%s]: %s:%i --> %s?%s" % (method, host, port, url, qs)) req = HTTP(host, port) # Set request-headers. if method.upper() == 'GET': if len(qs) > 0: qs = '?' + qs req.putrequest(method, url + qs) req.putheader('Host', host) authtobasic(auth, req) req.putheader('Accept', '*/*') req.endheaders() elif method.upper() == 'POST': req.putrequest(method, url) req.putheader('Host', host) authtobasic(auth, req) req.putheader('Accept', '*/*') req.putheader('Content-type', 'application/x-www-form-urlencoded') req.putheader('Content-length', '%d' % len(qs)) req.endheaders() # Send query string req.send(qs) # Send request. reply_code, message, headers = req.getreply() #### get parameter from content if reply_code == 404 or reply_code >= 500: error = "[%i]: %s at %s [%s]" % (reply_code, message, url, method) writeLog(self, "[http_import.error]: %s" % error) raise zExceptions.InternalError(error) elif reply_code == 200: # get content f = req.getfile() content = f.read() f.close() rtn = None if parse_qs: try: # return dictionary of value lists rtn = cgi.parse_qs(content, keep_blank_values=1, strict_parsing=1) except: # return string rtn = content else: # return string rtn = content if port != 80: rtn = rtn.replace('%s%s/' % (HTTP_PREFIX, host), '%s%s:%i/' % (HTTP_PREFIX, host, port)) return rtn else: result = '[' + str(reply_code) + ']: ' + str(message) writeLog(self, "[http_import.result]: %s" % result) return result
def __call__(self, *args, **kw): method = self.method if method == "PUT" and len(args) == 1 and not kw: query = [args[0]] args = () else: query = [] for i in range(len(args)): try: k = self.args[i] if kw.has_key(k): raise TypeError, "Keyword arg redefined" kw[k] = args[i] except IndexError: raise TypeError, "Too many arguments" headers = {} for k, v in self.headers.items(): headers[translate(k, dashtrans)] = v method = self.method if headers.has_key("Content-Type"): content_type = headers["Content-Type"] if content_type == "multipart/form-data": return self._mp_call(kw) else: content_type = None if not method or method == "POST": for v in kw.values(): if hasattr(v, "read"): return self._mp_call(kw) can_marshal = type2marshal.has_key for k, v in kw.items(): t = type(v) if can_marshal(t): q = type2marshal[t](k, v) else: q = "%s=%s" % (k, quote(v)) query.append(q) url = self.rurl if query: query = join(query, "&") method = method or "POST" if method == "PUT": headers["Content-Length"] = str(len(query)) if method != "POST": url = "%s?%s" % (url, query) query = "" elif not content_type: headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Content-Length"] = str(len(query)) else: method = method or "GET" if self.username and self.password and not headers.has_key("Authorization"): headers["Authorization"] = "Basic %s" % gsub( "\012", "", encodestring("%s:%s" % (self.username, self.password)) ) try: h = HTTP() h.connect(self.host, self.port) h.putrequest(method, self.rurl) for hn, hv in headers.items(): h.putheader(translate(hn, dashtrans), hv) h.endheaders() if query: h.send(query) ec, em, headers = h.getreply() response = h.getfile().read() except: raise NotAvailable, RemoteException(NotAvailable, sys.exc_value, self.url, query) if ec == 200: return (headers, response) self.handleError(query, ec, em, headers, response)
('Accept-Language', 'en'), ('Pragma', 'no-cache'), ('Cache-Control', 'no-cache'), ('User-Agent', 'Lynx/2.6 libwww-FM/2.14'), ('From', '"a-fan" <*****@*****.**>'), ('Referer', 'http://www.comcentral.com/mst/mstpoll.htm'), ('Content-type', 'application/x-www-form-urlencoded'), ('Content-length', '8')] text='vote=' + str(episode) n = n - 1 h = HTTP('www.comcentral.com') h.debuglevel = 1 h.putrequest('POST', '/cgi-bin/rbox/pollboy.pl') for (hn, hv) in headers: if (hn == 'From'): hv = 'user' + str(n) + '@blah.com' h.putheader(hn, hv) #print "hn:", hn, "hv:", hv h.endheaders() h.send(text) errcode, errmsg, headers = h.getreply() if errcode == 200: f = h.getfile() print f.read() # Print the raw HTML