Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从http请求中分离URL_Python_Twisted - Fatal编程技术网

Python 从http请求中分离URL

Python 从http请求中分离URL,python,twisted,Python,Twisted,我正在学习Python语言。我想知道有关拆分HTTP请求的信息 GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1 Host: www.explainth.at User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11 Accept: text/xml,text/html;q

我正在学习Python语言。我想知道有关拆分HTTP请求的信息

GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1

Host: www.explainth.at
User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11
Accept: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.explainth.at/en/misc/httpreq.shtml
我想在GET和Host之后合并该部分(用粗体字母)

GET/en/html/dummy.php?name=MyName&marred=not+single&male=yesHTTP/1.1

主持人:www.explainth.at


如何执行?

您必须按
\r\n
字节分割HTTP请求。(windows上的换行标记)

不清楚为什么要这样做,上下文或目标是什么,或者这些数据是如何到达程序的。但是,Python在其字符串类型上支持许多有用的字符串操作。因此,如果您有一个包含所有这些文本的字符串,那么您可能会发现splitlines方法以及一些列表切片非常有用:

s=”“”\ …GET/en/html/dummy.php?name=MyName&married=not+single&male=yes HTTP/1.1 …主持人:www.explainth.at …用户代理:Mozilla/5.0(Windows;en-GB;rv:1.8.0.11)Gecko/20070312 Firefox/1.5.0.11 …接受:text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5 ... """ s、 拆分线()[:2] ['GET/en/html/dummy.php?name=MyName&married=not+single&male=yes HTTP/1.1','Host:www.explainth.at']

当然,如果您正在编写任何类型的真正的HTTP服务器软件,这可能不是正确的方法(几乎没有理由在如此低的级别上运行,如果需要,您几乎肯定希望编写或重新使用真正的HTTP解析器)。所以你可能想问一个更精确的问题