Code a simple telnet client using sockets in python
测试端口是否开放的python脚本
原文: https://www.binarytides.com/code-telnet-client-sockets-python/
配置: 120.79.14.81:81/test.php 下面会返回数据。
telnet 80 不能获取到81端口下的web服务的数据。

telnet 81 就可以发送数据了,

nginx 的access.log的日志:

--------------------------------------------------------------------
The telnet client is a simple commandline utility that is used to connect to socket servers and exchange text messages. Here is an example of how to use telnet to connect to google.com and fetch the homepage.
$ telnet google.com 80
The above command will connect to google.com on port 80.
$ telnet google.com 80
Trying 74.125.236.69...
Connected to google.com.
Escape character is '^]'.
Now that it is connected, the telnet command can take user input and send to the server, and whatever the server replies with, will be displayed on the terminal. For example send the http GET command and hit enter twice.
GET / HTTP/1.1
Sending the above will generate some response from the server. Now we are going to make a similar telnet program in python. The program is short and simple. To implement a program that takes user input and fetches results from the remote server at the same, requires somekind of parallel processing. Now the obvious solution to this is to use threads. One thread to keep receiving message from server and another to keep taking in user input. But there is another way to do this apart from threads. And that is select function. Select function allows to monitor multiple sockets/streams for readability and will generate an even if any of the sockets is ready.
Lets take a look at the full code. Its less than 50 lines including comments.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# telnet program exampleimport socket, select, string, sys#main functionif __name__ == "__main__": if(len(sys.argv) < 3) : print 'Usage : python telnet.py hostname port' sys.exit() host = sys.argv[1] port = int(sys.argv[2]) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(2) # connect to remote host try : s.connect((host, port)) except : print 'Unable to connect' sys.exit() print 'Connected to remote host' while 1: socket_list = [sys.stdin, s] # Get the list sockets which are readable read_sockets, write_sockets, error_sockets = select.select(socket_list , [], []) for sock in read_sockets: #incoming message from remote server if sock == s: data = sock.recv(4096) if not data : print 'Connection closed' sys.exit() else : #print data sys.stdout.write(data) #user entered a message else : msg = sys.stdin.readline() s.send(msg) |
Small program! Just run it in a terminal like this
$ python telnet.py google.com 80
Connected to remote host
Once connected it shows the connected message. Once the message pops up, its time to type in some message to send to the remote server. Type the same GET message and send by hitting enter twice. Some response should be generated.
The above program does the task of listening for message from remote server and listening for user input at the same time, and without threads.
|
1
2
3
4
|
socket_list = [sys.stdin, s] # Get the list sockets which are readableread_sockets, write_sockets, error_sockets = select.select(socket_list , [], []) |
The socket list contains 2 sockets. First is the sys.stdin which is stream for standard input or the user input at the command line. The other one is the socket that is connected to remote server. The select function keeps listening on both of them. It is a blocking function and returns if either of the 2 things happens
1. Server sends a message
2. User hits enter after typing in a message
If the server socket is ready to be read, then just call recv function on it. If the user input is ready to be read then call sys.stdin.readline() function get the user message. Thats all about it.
The telnet client shown above is a minimal one. The actual telnet client has lots of other features which you can try to implement. The above telnet client can be used as a terminal chat client as well with little modifications. Just have to write a chat server. Will come up with a post on that soon.
Note
The above shown program will work only on linux and not on windows. The program uses the select function read the command line input (stdin). On windows the select function cannot read file descriptors. It can only read sockets created inside winsock. The python documentation on selectfunction mentions this
File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.
Code a simple telnet client using sockets in python的更多相关文章
- A Telnet Client Using Expect
The following expect script achieves a simple telnet client: login -> send command -> exit. Th ...
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- A simple Test Client built on top of ASP.NET Web API Help Page
Step 1: Install the Test Client package Install the WebApiTestClient package from the NuGet Package ...
- Python CODE——Nonblocking I/O client AND Delaying Server
#!Nonblocking I/O - Chapter 5 -pollclient.py import socket,sys,select port=51423 host='localhost' sp ...
- [文摘]Quick Start to Client side COM and Python
摘自:PyWin32.chm Introduction This documents how to quickly start using COM from Python. It is not a t ...
- Segger RTT : Real Time Terminal SRAM 调试解决方法
http://segger.com/jlink-real-time-terminal.html Real Time Terminal SEGGER's Real Time Terminal (RTT) ...
- OData Client Code Generator
转发. [Tutorial & Sample] How to use OData Client Code Generator to generate client-side proxy cla ...
- 【RL-TCPnet网络教程】第32章 RL-TCPnet之Telnet服务器
第32章 RL-TCPnet之Telnet服务器 本章节为大家讲解RL-TCPnet的Telnet应用,学习本章节前,务必要优先学习第31章的Telnet基础知识.有了这些基础知识之后,再搞 ...
- Code Project精彩系列(转)
Code Project精彩系列(转) Code Project精彩系列(转) Applications Crafting a C# forms Editor From scratch htt ...
随机推荐
- Linux与其它类Unix内核的比较
单块结构的内核:由几个逻辑上独立的成分构成,单块结构,大多数据商用Unix变体也是单块结构: 编译并静态连接的传统Unix内核:Linux能自动按需动态地装载和卸载部分内核代码(模块),而传统Unix ...
- Java 深入浅出String
String String是一个被final修饰的类,直接继承于Object,同时也实现了charsequence接口,String被声明为final也就不可以被继承了.由于String的方法比较多, ...
- EOJ 3265 七巧板
模拟. 先判断三边形和四边形的个数. 然后判断$5$个三角形是否都是等腰直角三角形. 然后判断$5$个等腰直角三角形比例是否符合要求. 然后寻找正方形.判断比例是否符合要求. 最后判断四边形是否符合要 ...
- POJ1284 Primitive Roots [欧拉函数,原根]
题目传送门 Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5434 Accepted: ...
- 备份Kylin的Metadata
元数据是Kylin中最重要的数据之一,备份元数据时运维工作中一个至关重要的环节.只有这样,在由于误操作导致整个Kylin服务或某个Cube异常时,才能将Kylin快速从备份中恢复出来. Kylin组织 ...
- [BZOJ5361]/[HDU6291]对称数
[BZOJ5361]/[HDU6291]对称数 题目大意: 一个\(n(n\le2\times10^5)\)个结点的树,每个结点有一个权值\(a_i(a_i\le2\times10^5)\),\(m( ...
- SpringBoot 搭建简单聊天室
SpringBoot 搭建简单聊天室(queue 点对点) 1.引用 SpringBoot 搭建 WebSocket 链接 https://www.cnblogs.com/yi1036943655/p ...
- 洛谷P1341 最受欢迎的奶牛
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- python开发_dbm_键值对存储_完整_博主推荐
''' 在python的应用程序中,不需要关系型数据库时,如MySQL 可以使用python提供的持久字典dbm来存储名称和值(键值对) 这个类似于java的中的java.util.Map对象. 区别 ...
- git fetch, git pull 以及 FETCH_HEAD
git push. 这个很简单, 其实和后面的差不多, 这里就不讲了. 唯一需要注意的地方是: git push origin :branch2, 表示将一个内容为空的同名分支推送到远程的分支.(说白 ...