如何在 Perl 脚本中重新建立网络连接?
我有一个 Perl 代码,可以访问特定网站并从中提取所需的数据。如果失去连接,我会遇到一个问题:脚本停止运行。我怎样才能让 Perl 脚本恢复连接并从中断发生的地方开始进程?我可以使用 Perl 恢复连接吗?如果可以的话,请有人指导我执行这些步骤。
I have a Perl code which goes to particular website and extracts the required data from it. I face a problem if I lose my connection: the script stops functioning. How could I make the Perl script resume the connection and start the process from where the interruption took place? Can I use the Perl to resume the connection if so could any one guide me with the steps please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的。一旦传输层网络(例如 TCP/IP)连接断开,您就无法再将其恢复。并且 HTTP 协议没有提供执行此操作的更高级别的方法。
您必须打开到服务器的新 HTTP 连接并重新启动提取。您是否必须从头开始重新启动,或者可以恢复到连接中断时的位置,取决于您要提取的内容、网站如何呈现它以及您用于提取的客户端工具。
It is not possible to do this. Once a transport-level network (e.g. TCP/IP) connection is broken you cannot get it back again. And the HTTP protocol does not provide a higher level way of doing this.
You have to open a new HTTP connection to the server and restart your extraction. Whether you have to restart from the beginning, or can resume close to where you were when the connection broke depends on what it is you are fetching, how the website presents it, and the client-side tool you are using to do the fetching.
如果您的意思是您已丢失拨号连接并且您使用的是 Windows,则答案是调用 rasdial 命令使用 系统() 。
On the off chance that you mean you've lost your dialup connection and you are on windows, the answer is to call the rasdial command using system().
当您说“失去连接”和“访问网站并提取数据”时,是什么意思?您的 Perl 代码是否管理套接字连接和数据抓取本身,或者您是否使用更高级别的 Perl 库,例如 LWP 或 WWW::Mechanize 来“提取数据”
如果后者那么您可能想阅读这个问题和答案
What do you mean when you say "lose my connection" and "goes to a website and extracts data"? Is your perl code managing the Socket Connection and the data scraping itself or are you using a higher level Perl library like LWP or WWW::Mechanize to "extract data"
If the latter then you might like to read this question and the answers