我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cgi.parse_header()。
def do_POST(self): try: self.send_response(301) self.send_header('Content-type', 'text/html') self.end_headers() ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('message') output = "" output += "<html><body>" output += " <h2> Okay, how about this: </h2>" output += "<h1> %s </h1>" % messagecontent[0] output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>''' output += "</body></html>" self.wfile.write(output) print output except: pass
def get_encoding_from_headers(headers): """Returns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. :rtype: str """ content_type = headers.get('content-type') if not content_type: return None content_type, params = cgi.parse_header(content_type) if 'charset' in params: return params['charset'].strip("'\"") if 'text' in content_type: return 'ISO-8859-1'
def __init__(self, content, url, headers=None): # Determine if we have any encoding information in our headers encoding = None if headers and "Content-Type" in headers: content_type, params = cgi.parse_header(headers["Content-Type"]) if "charset" in params: encoding = params['charset'] self.content = content self.parsed = html5lib.parse( self.content, transport_encoding=encoding, namespaceHTMLElements=False, ) self.url = url self.headers = headers
def get_single_header(headers, key): """ Get a single value for the given key out of the given set of headers. :param twisted.web.http_headers.Headers headers: The set of headers in which to look for the header value :param str key: The header key """ raw_headers = headers.getRawHeaders(key) if raw_headers is None: return None # Take the final header as the authorative header, _ = cgi.parse_header(raw_headers[-1]) return header
def get_encoding_from_headers(headers): """Returns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. """ content_type = headers.get('content-type') if not content_type: return None content_type, params = cgi.parse_header(content_type) if 'charset' in params: return params['charset'].strip("'\"") if 'text' in content_type: return 'ISO-8859-1'
def checkforhtml(self, info, url): # if info.has_key('content-type'): # ctype = cgi.parse_header(info['content-type'])[0].lower() # if ';' in ctype: # # handle content-type: text/html; charset=iso8859-1 : # ctype = ctype.split(';', 1)[0].strip() # else: # if url[-1:] == "/": # return 1 # ctype, encoding = mimetypes.guess_type(url) # if ctype == 'text/html': # return 1 # else: # self.note(1, " Not HTML, mime type %s", ctype) # return 0 return 1
def __init__(self, content, url, headers=None): # Determine if we have any encoding information in our headers encoding = None if headers and "Content-Type" in headers: content_type, params = cgi.parse_header(headers["Content-Type"]) if "charset" in params: encoding = params['charset'] self.content = content self.parsed = html5lib.parse( self.content, encoding=encoding, namespaceHTMLElements=False, ) self.url = url self.headers = headers
def do_POST(self): try: if self.path.endswith("/restaurants/new"): ctype, pdict = cgi.parse_header( self.headers.getheader('content-type')) if ctype == 'multipart/form-data': fields = cgi.parse_multipart(self.rfile, pdict) messagecontent = fields.get('newRestaurantName') # Create new Restaurant Object newRestaurant = Restaurant(name=messagecontent[0]) session.add(newRestaurant) session.commit() self.send_response(301) self.send_header('Content-type', 'text/html') self.send_header('Location', '/restaurants') self.end_headers() except: pass
def __init__(self, url, name=None, requests_session=None, timeout=30): IOBase.__init__(self) self.url = url self.sess = requests_session if requests_session is not None else requests.session() self._seekable = False self.timeout = timeout f = self.sess.head(url, headers={'Range': 'bytes=0-'}, timeout=timeout) if f.status_code == 206 and 'Content-Range' in f.headers: self._seekable = True self.len = int(f.headers["Content-Length"]) if name is None: if "Content-Disposition" in f.headers: value, params = cgi.parse_header(f.headers["Content-Disposition"]) if "filename" in params: self.name = params["filename"] else: self.name = name f.close() self._pos = 0 self._r = None