Python 套接字数据返回目的。如何正则表达式它?

发布于 2024-08-22 16:25:59 字数 1189 浏览 3 评论 0原文

我正在用 python (3) 编写一个基本的 html 代理,到目前为止我还没有使用像 http.server 这样的预构建类。

我刚刚启动一个接受连接的套接字:

self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listen_socket.bind((socket.gethostname(), 4321))
self.listen_socket.listen(5)
(a, b) = self.listen_socket.accept()
content = a.recv(100000)

现在内容存储数据,例如:

b'GET http://www.google.com/firefox HTTP/1.1\r\nHost: www.google.com\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100207 Namoroka/3.6\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 115\r\nProxy-Connection: keep-alive\r\nCookie: PREF=ID=1ac935f4d893f655:U=73a4849dc5fc23a4:TM=1266851688:LM=1267023171:S=Log1PmXRMlNjX3Of; NID=32=EnrZjTqILuW2_aMLtgsJ96FdEMF3s5FoMJSVq9GMr9dhLhTAd3F5RcQ3ImyVBiO2eYNKKMhzlGg7r8zXmeSq50EigS5sdKtCL9BMHpgCxZazA2NiyB0bTRWhp8-0BObn\r\n\r\n'

如何对其进行正则表达式?转换为字符串对我来说不起作用。

或者,最终,我需要找出所查询的地址,例如本例中的 http://www.google.com/firefox 。有我不知道的解析器吗?我怎样才能达到结果?

提前致谢。

I'm writing a basic html-proxy in python (3), and up to now I'm not using prebuild classes like http.server.

I'm just starting a socket which accepts connection:

self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listen_socket.bind((socket.gethostname(), 4321))
self.listen_socket.listen(5)
(a, b) = self.listen_socket.accept()
content = a.recv(100000)

Now content stores data like:

b'GET http://www.google.com/firefox HTTP/1.1\r\nHost: www.google.com\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100207 Namoroka/3.6\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 115\r\nProxy-Connection: keep-alive\r\nCookie: PREF=ID=1ac935f4d893f655:U=73a4849dc5fc23a4:TM=1266851688:LM=1267023171:S=Log1PmXRMlNjX3Of; NID=32=EnrZjTqILuW2_aMLtgsJ96FdEMF3s5FoMJSVq9GMr9dhLhTAd3F5RcQ3ImyVBiO2eYNKKMhzlGg7r8zXmeSq50EigS5sdKtCL9BMHpgCxZazA2NiyB0bTRWhp8-0BObn\r\n\r\n'

How can I regexp it? Converting to string does not work for me.

Or, eventually, I need to find out the address which is inquired, like http://www.google.com/firefox in this case. Is there a parser that I do not know? How can I achieve the result?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-08-29 16:25:59

转换为字符串时需要包含编码,例如使用:

>>> str(b'GET http://...', 'UTF-8')
'GET http://...'

如果您不使用编码,那么正如您发现的那样,您会得到一些不太有用的东西:

>>> str(b'GET http://...')
"b'GET http://...'"

You need to include an encoding when converting to a string, for example use:

>>> str(b'GET http://...', 'UTF-8')
'GET http://...'

If you don't use an encoding then as you've discovered you get something a little less helpful:

>>> str(b'GET http://...')
"b'GET http://...'"
带刺的爱情 2024-08-29 16:25:59

另外,您可能需要检查 *HTTPServer 类。它们提供了 HTTP 服务器的包装器,并且还会为您解析标头。

如果您不能,那么,至少他们会提供有关如何做到这一点的源代码示例!

Also, you might want to check the *HTTPServer classes. They provide a wrapper around being HTTP servers and will also parse headers for you.

If you can't, well, at the very least they will provide source code examples on how to do it!

半窗疏影 2024-08-29 16:25:59

提供了在字节和字符串之间进行转换的方法,请尝试 str.encode() 和 bytes.decode()

http://python.about.com/od/python30/ss/30_strings_3.htm

Methods are provided to convert between bytes and strings try str.encode() and bytes.decode()

http://python.about.com/od/python30/ss/30_strings_3.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文