脚本语言如何使用套接字? [英] How do scripting languages use sockets?

查看:45
本文介绍了脚本语言如何使用套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python、Perl 和 PHP,都支持 TCP 流套接字.但究竟如何在由网络服务器(例如 Apache)运行的脚本文件中使用套接字,假设我只有 FTP 访问权限而不是对机器的 root 访问权限?

Python, Perl and PHP, all support TCP stream sockets. But exactly how do I use sockets in a script file that is run by a webserver (eg Apache), assuming I only have FTP access and not root access to the machine?

  1. 当客户端连接到特定端口时,如何调用脚本文件?

  1. When a client connects to a specific port, how does the script file get invoked?

脚本是否在连接期间保持运行"状态?(可能是几个小时)

Does the script stay "running" for the duration of the connection? (could be hours)

那么脚本的多个实例"会同时运行吗?

So will multiple "instances" of the script be running simultaneously?

那么如何从脚本的一个实例到另一个实例进行方法调用?

Then how can method calls be made from one instance of the script to another?

推荐答案

脚本语言使用套接字的方式与编译语言完全相同.

Scripting languages utilize sockets exactly the same way as compiled languages.

1) 脚本通常会打开并使用套接字.它不是由套接字运行"或调用",而是通过库直接控制它(通常调用操作系统的本机 C API).

1) The script typically opens and uses the socket. It's not "run" or "invoked" by the socket, but directly controls it via libraries (typically calling into the native C API for the OS).

2) 是的.

3) 不一定.大多数现代脚本语言可以在一个脚本"应用程序中处理多个套接字.

3) Not necessarily. Most modern scripting langauges can handle multiple sockets in one "script" application.

4) 不适用,见 3)

4) N/A, see 3)

根据问题和评论的变化进行

Edit in response to change in question and comments:

现在很明显您正在尝试在托管服务器的上下文中运行它.通常,如果您在 Apache 或类似服务器中使用脚本,则工作方式会有所不同.Apache 打开并维护一个套接字,它执行您的脚本,将相关数据(POST/GET 结果等)传递给您的脚本进行处理.当您为 CGI 等编写脚本时,套接字通常不会发挥作用.

This is now obvious that you are trying to run this in the context of a hosted server. Typically, if you're using scripting within Apache or a similar server, things work a bit differently. A socket is opened up and maintained by Apache, and it executes your script, passing the relevant data (POST/GET results, etc.) to your script to process. Sockets usually don't come into play when you're dealing with scripting for CGI, etc.

然而,这通常使用与 mod_cgi 相同的概念发生.这几乎意味着就服务器而言,运行的脚本只不过是一个可执行文件,而可执行文件的输出是返回给客户端的内容.在这种情况下(前提是您在服务器上拥有权限和正确的库),您的 Python 脚本实际上可以启动一个单独的脚本,该脚本完全在 Apache 的上下文之外执行自己的套接字工作.

However, this typically happens using the same concepts as mod_cgi. This pretty much means that the script running is nothing but an executable as far as the server is concerned, and the executable's output is what gets returned to the client. In this case, (provided you have permissions and the correct libraries on the server), your python script can actually launch a separate script that does its own socket work completely outside of Apache's context.

然而,(通常)直接在 CGI 脚本内部运行完整的套接字实现并不是一个好主意.CGI 将期望可执行文件在将结果返回给客户端之前运行完成.Apache 将坐在那里并挂起"一点等待此操作完成.如果您正在启动一个完整的服务器(特别是如果它是一个长时间运行的进程,它们往往是),Apache 会认为脚本被锁定,并且可能会中止,可能会终止该进程(特定于配置,但大多数托管公司都会这样做)防止脚本占用共享系统上的 CPU).

It's (usually) not a good idea to run a full socket implementation directly inside of the CGI script, however. CGI will expect the executable to run to completion before it returns results to the client. Apache will sit there and "hang" a bit waiting for this to complete. If you're launching a full server (especially if it's a long running process, which they tend to be), Apache will think the script is locked, and probably abort, potentially killing the process (configuration specific, but most hosting companies do this to prevent scripts from taking over CPU on a shared system).

但是,如果您从脚本中执行一个新脚本,然后返回(关闭 CGI 可执行文件),另一个脚本可以继续运行,作为服务器工作.这将类似于(python 示例,使用 subprocess 库):

However, if you execute a new script from within your script, and then return (shutting down the CGI executable), the other script can be left running, working as a server. This would be something like (python example, using the subprocess library):

newProccess = Popen("python MyScript", shell=True)

请注意,尽管如此,以上所有内容确实有点取决于服务器配置.许多托管公司没有在他们的脚本实现中包含一些套接字或 shell 库专门用于防止这种情况发生,因此您通常必须恢复使用 C 语言制作可执行文件.此外,这通常违反大多数托管公司的服务条款- 你必须检查你的.

Note that all of the above really depends a bit on server configuration, though. Many hosting companies don't include some of the socket or shell libraries in their scripting implementations specifically to prevent this, so you often have to revert to making the executable in C. In addition, this is often against terms of service for most hosting companies - you'd have to check yours.

这篇关于脚本语言如何使用套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆