如何使用 netcat/socat/telnet 手动连接到 websocket? [英] How do I connect to a websocket manually, with netcat/socat/telnet?

查看:30
本文介绍了如何使用 netcat/socat/telnet 手动连接到 websocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试手动"连接到参考 websocket 回显服务器,以了解协议的工作原理(我为此使用了 socat).但是,服务器总是在不提供答案的情况下关闭连接.知道为什么吗?

I am trying to connect to the reference websocket echo server "manually", in order to learn how the protocol works (I am using socat for that). However, the server invariably closes the connection without providing an answer. Any idea why?

这是我所做的:

socat - TCP:echo.websocket.org:80

然后,我在终端中粘贴以下文本:

Then, I paste the following text in the terminal:

GET /?encoding=text HTTP/1.1
Origin: http://www.websocket.org
Connection: Upgrade
Host: echo.websocket.org
Sec-WebSocket-Key: P7Kp2hTLNRPFMGLxPV47eQ==
Upgrade: websocket
Sec-WebSocket-Version: 13

我在同一台机器上的 firefox 中嗅探了与开发人员工具的连接参数,它完美无缺:因此,我认为它们是正确的.然而,在那之后,服务器立即关闭连接,不提供答案.为什么?如何手动"实现协议?

I sniffed the parameters of the connection with the developer tools, in firefox, on the same machine, where this works flawlessly: therefore, I would assume they are correct. However after that, the server closes the connection immediately, without providing an answer. Why? How can I implement the protocol "manually"?

我想在我的终端中输入 test 并让服务器回复我输入的内容(它适用于网络浏览器).

I would like type test in my terminal and get the server to reply with what I typed (It works in a web browser).

推荐答案

我想你想修改套接字流以将 (换行)转换为 CRLF(回车和换行).执行 info socat 会生成包含此修饰符的详细信息:

I think you want to modify the socket stream to translate (line feed) to CRLF (Carriage return & line feed). Doing info socat produces detailed information which includes this modifier:

crnl   Converts the default line termination character NL ('
',  0x0a)
       to/from CRNL ("
", 0x0d0a) when writing/reading on this chan-
       nel (example).  Note: socat simply strips all CR characters.

所以我认为你应该能够做到这一点:

So I think you should be able to do this:

socat - TCP:echo.websocket.org:80,crnl

这篇关于如何使用 netcat/socat/telnet 手动连接到 websocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆