Convert the input from telnet to a list in twisted
input from telnet
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
how can i get this input into a list.....?
i want like
a = ['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,*/*开发者_StackOverflow;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']
this is an http request received from telnet.
i'm using EchoProtocol(basic.LineReceiver)
.
Assuming you're getting those lines of text from a text file-like object f
(maybe sys.stdin
, whatever), list(f)
or f.readlines()
are almost what you want except that there are line-end markers at the end of each line. f.read().split('\n')
may be closer to what you want (the same split
call works if you have the text as a string s
coming from some other source, s.split('\n')
is the list you want).
If you've read any of the LineReceiver
documentation, then you should have seen that all received lines are passed to the lineReceived
callback method of that class. So the answer to your question is a class that looks something like this:
from twisted.protocols.basic import LineReceiver
class LineCollector(LineReceiver):
def connectionMade(self):
self.lines = []
def lineReceived(self, line):
self.lines.append(line)
This gives you just what you asked for - your input in a list, one line per entry. However, it's far from clear why you want this. If you actually want to generate an HTTP response, this is the wrong way to go about doing so.
精彩评论