0
votes

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,*/*;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).

2
Can you describe what you want the list to look like?Ned Batchelder
That's obviously a HTTP GET request, but the question needs more context. Where is this coming from, and what are you going to do with it? If it were a HTTP request to Twisted Web, you'd be dealing with this on a higher level of abstraction.Matthew Flaschen
please learn to format your text. putting four spaces before a line will mark it as code and keep new lines from collapsing.aaronasterling
@Aaron, how to format? i didn't get it? can u specify a little more...asha
when you click 'edit', you will see a preview beneath the textfield. Play around with it and make it readable before you post. It uses markdown so you can google for that. The main thing is that four spaces at the start of a line marks that line as code.aaronasterling

2 Answers

1
votes

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).

0
votes

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.