Convert the input from telnet to a list in twisted

こ雲淡風輕ζ 提交于 2019-12-11 14:17:52

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/3732345/convert-the-input-from-telnet-to-a-list-in-twisted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!