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 ...
随机推荐
- Python并发编程-一个简单的多进程实例
import time from multiprocessing import Process import os def func(args,args2): #传递参数到进程 print(args, ...
- LongAdder & AtomicInteger
JDK8 推荐 LongAdder替代 AtomicInteger, AtomicInteger内部是实现使用 (网友使用jad反编译源码 参考 http://ifeve.com/enhanced- ...
- 洛谷——P1330 封锁阳光大学
P1330 封锁阳光大学 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构 ...
- PlayMaker GUI跟随布局的使用
PlayMaker GUI跟随布局的使用 PlayMaker提供一种的特殊的GUI布局方式GUI Layout (Begin) Area Follow Object.这种布局可以在特定游戏对象上显 ...
- go语言学习框架
根据框架学习 https://books.studygolang.com/gopl-zh/ 看电子书 http://shinley.com/index.html
- 2016 湖南省省赛B题《有向无环图》
题目链接[https://vjudge.net/problem/CSU-1804] 题意: 给出一个有向无环图,然后让你算下面的结果,count(i,j)表示i->j之间的路径条数. 题解: 根 ...
- 我的OI生涯 第二章
第二章 很快就开学了,我们几个到了16班,开哥茂嵩到了15班,MJX和ZZJ去了四班,YST去了九班. 竞赛课改成了一周两节,每周都学习一点新的语言,这期间来了一些新同学例如于北,LTY,他们刚来感觉 ...
- [HDU4609]3-idiots(生成函数+FFT)
3-idiots Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- CVE-2017-7269—IIS 6.0 WebDAV远程代码执行漏洞分析
漏洞描述: 3月27日,在Windows 2003 R2上使用IIS 6.0 爆出了0Day漏洞(CVE-2017-7269),漏洞利用PoC开始流传,但糟糕的是这产品已经停止更新了.网上流传的poc ...
- Java高级架构师(一)第43节:Varnish简介、安装和基本使用
第一部分:Varnish简介 Varnish是一款开源的.高性能的HTTP加速器和反向代理服务器. Varnish反向代理的能力远不如Nginx. Varnish主要作用是HTTP的加速器,主要通过缓 ...