使用python将其转换为字符串

使用python将其转换为字符串,python,Python,如何将此输入转换为字符串 GET /learn/tutorials/351079-weekend-project-secure-your-system-with-port-knocking?name=MyName&married=not+single&male=yes HTTP/1.1 Host: merch1.localhost User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.1

如何将此输入转换为字符串

GET /learn/tutorials/351079-weekend-project-secure-your-system-with-port-knocking?name=MyName&married=not+single&male=yes HTTP/1.1 Host: merch1.localhost 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 GET/learn/tutorials/351079 weekend project使用端口敲门保护您的系统?name=MyName&marred=not+single&male=yes HTTP/1.1 主机:merch1.localhost 用户代理: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,文本/普通;q=0.8,图像/png,*/*;q=0.5 接受语言:en-gb,en;q=0.5 接受编码:gzip,deflate 接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7 活命:300 连接:保持活力 我需要输出为:


GET/learn/tutorials/351079 weekend project使用端口敲门保护您的系统?name=MyName&marred=not+single&male=yes HTTP/1.1主机:merch1.localhost用户代理: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,文本/普通;q=0.8,图像/png,*/*;q=0.5接受语言:en gb,en;q=0.5接受编码:gzip,deflate接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7保持活动状态:300连接:保持活动状态

只需将输入拆分为多行并连接它们:

ins = """\
GET /learn/tutorials/351079-weekend-project-secure-your-system-with-port-knocking?name=MyName&married=not+single&male=yes HTTP/1.1
Host: merch1.localhost
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
"""

outs = 'GET /learn/tutorials/351079-weekend-project-secure-your-system-with-port-knocking?name=MyName&married=not+single&male=yes HTTP/1.1 Host: merch1.localhost 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'

assert( ' '.join(ins.splitlines()) == outs)

你在哪里收到这些信息?你尝试了什么?我在这里看到的唯一转换的可能重复是从标题中删除换行符。这就是你想要实现的吗?另外,我注意到输出中有一个
HTTP/1.1
,它不在输入的任何地方。@Phoenix:我在第一行末尾看到
HTTP/1.1
;在我看来,他只是在用空格字符替换每条换行符,即
s.replace('\r\n','')