如何在Python中将IP地址绑定到telnetlib
下面的代码将ip地址绑定到urllib、urllib2等。
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
sock.bind((sourceIP, 0))
return sock
socket.socket = bound_socket
是否也可以将ip地址绑定到telnetlib?
The code below binds an ip address to urllib, urllib2, etc.
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
sock.bind((sourceIP, 0))
return sock
socket.socket = bound_socket
Is it also able to bind an ip address to telnetlib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
telnetlib
至少在最近的 Python 版本中使用socket.create_connection
(请参阅 telnetlib 的源代码 这里),但这也应该被你的monkeypatch捕获(来源此处 - 您会看到它使用裸标识符socket
但这正是您正在猴子修补的模块中)。当然,monkeypatching 总是非常脆弱(未来版本中最微小的优化,在create_connection
中提升socket
的全局查找,然后你就完蛋了……;-)所以也许你会想直接使用 Monkeypathcreate_connection
作为一种稍微更强的方法。telnetlib
at least in recent Python releases usessocket.create_connection
(see telnetlib's sources here) but that should also be caught by your monkeypatch (sources here -- you'll see it uses a bare identifiersocket
but that's exactly in the module you're monkeypatching). Of course monkeypatching is always extremely fragile (the tiniest optimization in some future release, hoisting the global lookup ofsocket
increate_connection
, and you're toast...;-) so maybe you'll want to monkeypathcreate_connection
directly as a modestly-stronger approach.