Python 中的 Netcat 实现
我发现 这个 并使用它作为我的基础,但它不能立即使用。我的目标是将其视为一个包而不是命令行实用程序,因此我的代码更改将反映这一点。
class Netcat:
def __init__(self, hostname, port):
self.hostname = hostname
self.port = port
def send(self, content):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.hostname, self.port))
self.socket.setblocking(0)
result = '';
read_ready, write_ready, in_error = select.select([self.socket], [], [self.socket], 5)
if(self.socket.sendall(content) != None):
return
while(1):
buffer = ''
try:
buffer = self.socket.recv(128)
while(buffer != ''):
result += buffer
try:
buffer = self.socket.recv(128)
except socket.error as err:
print (err, type(err))
buffer = ''
if(buffer == ''):
break
except socket.error as err:
print (err, type(err))
if(buffer == ''):
break
return result
当我向我的设备发送基本命令时,它会返回以下内容。
50PMA-019 Connection Open
Atten #1 = 63dB
我的代码读取第一行,但随后出现错误,指出连接暂时不可用,并且无法获取第二行。如果我将其更改为阻塞,它只会阻塞并且永远不会返回。有什么想法吗?
I found this and am using it as my base, but it wasn't working right out of the box. My goal is also to treat it as a package instead of a command line utility, so my code changes will reflect that.
class Netcat:
def __init__(self, hostname, port):
self.hostname = hostname
self.port = port
def send(self, content):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.hostname, self.port))
self.socket.setblocking(0)
result = '';
read_ready, write_ready, in_error = select.select([self.socket], [], [self.socket], 5)
if(self.socket.sendall(content) != None):
return
while(1):
buffer = ''
try:
buffer = self.socket.recv(128)
while(buffer != ''):
result += buffer
try:
buffer = self.socket.recv(128)
except socket.error as err:
print (err, type(err))
buffer = ''
if(buffer == ''):
break
except socket.error as err:
print (err, type(err))
if(buffer == ''):
break
return result
When I send a basic command to my device, it returns the following.
50PMA-019 Connection Open
Atten #1 = 63dB
My code reads the first line, but then I get an error saying that the connection is temporarily unavailable and it does not get the second line. If I change it to blocking, it just blocks and never returns. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只使用
nc
是否有效?我认为您应该尝试一些更简单的方法:
我添加了
shutdown
调用,因为也许您的设备正在等待您说已完成发送数据。 (这有点奇怪,但这是可能的。)Does it work if you just use
nc
?I think you should try something a little simpler:
I added the
shutdown
call because maybe your device is waiting for you to say you're done sending data. (That would be a little weird, but it's possible.)以下是 python3 上的一个工作实现:
它可用于将“内容”发送到“端口”上的“主机”(所有这些都可以作为字符串输入)。
The following is a working implementation on python3:
It can be used to send "content" to a "host" on "port" (which all might be entered as string).
如果您不介意完全废弃该代码,您可能想看看 scapy --它基本上是Python中数据包工具中的瑞士军刀。查看交互式教程看看是否可以适合您的需求。
如果你想要比数据包更高级别的东西twisted是Python中网络的首选库..不幸的是,学习曲线有点陡峭。
if you don't mind scrapping that code altogether, you might like to look at scapy -- it's basically the swiss army knife of packet tools in python. take a look at the interactive tutorial to see if it fits your needs.
if you'd like something higher-level than packets twisted is the go-to library for networking in python... unfortunately the learning curve is a tad steep.