python urllib 添加函数到 headers 信息
python urllib add function to headers info
请原谅我缺乏知识,但我无法找到解决这个简单语法问题的方法。
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: str(uuid.uuid1())[:6]'
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')
我正在尝试像这样将此函数 str(uuid.uuid1())[:6] 添加到 'ID: id number' header 的 headers 中。但我遇到了不正确的语法问题。
您必须使用简单的 +
运算符连接字符串(将它们合并在一起)。您可以在任何使用字符串的地方执行此操作,
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: ' + str(uuid.uuid1())[:6]
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')
不想使用+
运算符的地方(会使代码看起来略显凌乱),同样可以使用str.format
方法,如下:
str.format
是一种可用于将变量插入字符串中任意位置的方法。只需在字符串中插入 {}
并在末尾使用 .format()
插入您需要添加的任何内容(当然作为 format
的参数!)
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: {}'.format(str(uuid.uuid1())[:6])
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')
请原谅我缺乏知识,但我无法找到解决这个简单语法问题的方法。
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: str(uuid.uuid1())[:6]'
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')
我正在尝试像这样将此函数 str(uuid.uuid1())[:6] 添加到 'ID: id number' header 的 headers 中。但我遇到了不正确的语法问题。
您必须使用简单的 +
运算符连接字符串(将它们合并在一起)。您可以在任何使用字符串的地方执行此操作,
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: ' + str(uuid.uuid1())[:6]
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')
不想使用+
运算符的地方(会使代码看起来略显凌乱),同样可以使用str.format
方法,如下:
str.format
是一种可用于将变量插入字符串中任意位置的方法。只需在字符串中插入 {}
并在末尾使用 .format()
插入您需要添加的任何内容(当然作为 format
的参数!)
requestHdr = (
'Host: amazon.com\r\n'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.4.0\r\n'
'Accept: */*\r\n'
'Accept-Language: en-US,en;q=0.5\r\n'
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n'
'Origin: http://amazon.com\r\n'
'Pragma: no-cache\r\n'
'ID: {}'.format(str(uuid.uuid1())[:6])
'Cache-Control: no-cache\r\n\r\n'
'Connection: Close\r\n')