简体   繁体   English

Python连接套接字进行处理

[英]Python connect socket to process

I have a ( very ) simple web server I wrote in C and I want to test it. 我有一个( 非常 )简单的Web服务器,我用C编写,我想测试它。 I wrote it so it takes data on stdin and sends out on stdout. 我写了它所以它需要stdin上的数据并发送到stdout。 How would I connect the input/output of a socket (created with socket.accept()) to the input/output of a process created with subprocess.Popen? 如何将套接字(使用socket.accept()创建)的输入/输出连接到使用subprocess.Popen创建的进程的输入/输出?

Sounds simple, right? 听起来很简单吧? Here's the killer: I'm running Windows. 这是杀手:我正在运行Windows。

Can anyone help? 有人可以帮忙吗?

Here's what I've tried: 这是我尝试过的:

  1. Passing the client object itself as stdin/out to subprocess.Popen. 将客户端对象本身作为stdin / out传递给subprocess.Popen。 (It never hurts to try.) (尝试从不痛苦。)
  2. Passing socket.makefile() results as stdin/out to subprocess.Popen. 将socket.makefile()作为stdin / out传递给subprocess.Popen。
  3. Passing the socket's file number to os.fdopen(). 将套接字的文件号传递给os.fdopen()。

Also, in case the question was unclear, here's a slimmed-down version of my code: 此外,如果问题不清楚,这里是我的代码的精简版本:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', PORT))
sock.listen(5)
cli, addr = sock.accept()
p = subprocess.Popen([PROG])
#I want to connect 'p' to the 'cli' socket so whatever it sends on stdout
#goes to the client and whatever the client sends goes to its stdin.
#I've tried:
p = subprocess.Popen([PROG], stdin = cli.makefile("r"), stdout = cli.makefile("w"))
p = subprocess.Popen([PROG], stdin = cli, stdout = cli)
p = subprocess.Popen([PROG], stdin = os.fdopen(cli.fileno(), "r"), stdout = os.fdopen(cli.fileno(), "w"))
#but all of them give me either "Bad file descriptor" or "The handle is invalid".

I had the same issue and tried the same way to bind the socket, also on windows. 我有同样的问题,并尝试以相同的方式绑定套接字,也在Windows上。 The solution I came out with was to share the socket and bind it on the process to stdin and stdout . 我提出的解决方案是共享套接字并将其绑定到进程到stdinstdout My solutions are completely in python but I guess that they are easily convertible. 我的解决方案完全是python,但我猜它们很容易转换。

import socket, subprocess

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', PORT))
sock.listen(5)
cli, addr = sock.accept()

process = subprocess.Popen([PROG], stdin=subprocess.PIPE)
process.stdin.write(cli.share(process.pid))
process.stdin.flush()

# you can now use `cli` as client normally

And in the other process: 在另一个过程中:

import sys, os, socket

sock = socket.fromshare(os.read(sys.stdin.fileno(), 372))
sys.stdin = sock.makefile("r")
sys.stdout = sock.makefile("w")

# stdin and stdout now write to `sock`

The 372 is the len of a measured socket.share call. 372是测量的socket.share调用的len I don't know if this is constant, but it worked for me. 我不知道这是不变的,但它对我有用。 This is possible only in windows, as the share function is only available on that OS. 这仅在Windows中可用,因为share功能仅在该OS上可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM