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

查看:216
本文介绍了如何使用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).

推荐答案

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

I think you want to modify the socket stream to translate \n (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 ('\n',  0x0a)
       to/from CRNL ("\r\n", 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天全站免登陆