【问题标题】:Python select with sockets and sys.stdin带有套接字和 sys.stdin 的 Python 选择
【发布时间】:2013-01-23 20:30:38
【问题描述】:

我是 Python 编程新手,我正在尝试创建服务器和客户端。我仍然希望能够从键盘输入一些东西,这样我就可以通过输入“退出”从服务器关闭服务器。我从各个站点获取了示例代码,以了解我在套接字编程和此代码方面所处的位置。

但是,每当我运行代码时,都会收到以下错误消息:

The host name of this machine is 127.0.0.1
The IP address of the host is 127.0.0.1
Server now awaiting client connection on port 2836
im right before the select
Traceback (most recent call last):
  File "/root/Server_2.py", line 42, in <module>
    inputready, outputready, exceptready = select.select(input, [], [])
TypeError: argument must be an int, or have a fileno() method.
>>> 

我正在阅读以通过这个(在 Windows 中)是删除 sys.stdin 因为 Windows 只接受套接字。我正在尝试在 Linux 中编写此代码。我已经尝试了各种方法来让它发挥作用,但我已经没有资源和想法可以尝试了。下面是服务器代码:

import socket                       #import socket module
import select
import sys

host = "127.0.0.1"
print ("The host name of this machine is " + host)
hostIP = socket.gethostbyname(host)    # get host IP address
print ("The IP address of the host is %s" % (hostIP)) 
port = 2836                        # Reserve the port for the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((hostIP, port))                # This server to a port

s.listen(4)                         # Now wait for client connection

print("Server now awaiting client connection on port %s" % (port))

#WINDOWS ONLY ACCEPTS SOCKETS FOR SELECT(), no standard in
input = [s, sys.stdin]


running = 1

while running:


    print("im right before the select")
    # when there's something in input, then we move forward
    # ignore what's in output and except because there's nothing
    # when it comes to sockets
    inputready, outputready, exceptready = select.select(input, [], [])

    print("i'm here na")
    # check who made a response
    for x in inputready:

        if x == s:
            print(s)
            #handle the server socket
            client, address = s.accept()
            print("connection comming in")
            input.append(client)

        elif x == sys.stdin:
            # handle standard input
            stuff = sys.stdin.readline()
            if stuff == "exit":
                running = 0
            else:
                print("you typed %s" % (stuff))


        else:
            #handle all other sockets
            data = x.recv(1024)
            print("i received " + data)
            if data:
                if data == "exit":
                    x.close()
                    input.remove(x)
                    running = 0
                else:
                    x.send(data)
                    print("I am sending %s" % (data))
            else:
                x.close()
                input.remove(x)


s.close()

任何帮助或想法将不胜感激。谢谢!!

【问题讨论】:

  • 你的代码在 Linux 上适用于我。
  • 这就是我的想法...我看到此代码类似于此站点,其他站点引用 sys.stdin 和 select.select。我的 Python 一定做错了什么。为了给我正在运行的更多参考,我买了一个 RaspberryPi 开发板,在 SD 卡中重新加载了 linux,它包含 Python3。是否有可能我需要配置我的 Python 或其他东西来获取 sys.stdin 并选择工作?我正在导入选择库....
  • 这是一个相关的讨论:stackoverflow.com/questions/3762881/…

标签: python sockets select client


【解决方案1】:

我知道你 7 年前问过这个问题,但我也有类似的问题,所以我想我会回答你。我仍在工作并修复具有相同功能的程序,但我知道的一件事是select.select() 中的参数列表需要是文件描述符(整数)。

所以如果你有这个块

input = [s, sys.stdin]


running = 1

while running:


    print("im right before the select")
    # when there's something in input, then we move forward
    # ignore what's in output and except because there's nothing
    # when it comes to sockets
    inputready, outputready, exceptready = select.select(input, [], [])

我要说的第一件事是将您的阅读列表更改为不是input。您可能会与 input() 函数发生冲突,这可能会导致令人困惑的错误。之后,您希望这些值是文件描述符。所以第一行应该是

inputSockets = [s.fileno(), sys.stdin.fileno()]

然后在检查哪个套接字准备就绪时,您会希望这样做

for x in inputready:
    if x == s.fileno():
        # Read from your s socket
    elif x == sys.stdin().fileno():
        # Read from stdin
    else:
        '''
        Here you would want to read from any other sockets you have.
        The only problem is your inputSockets array has ints, not socket
        objects. What I did was store an array of actual socket objects
        alongside the array of file descriptors. Then I looped through the
        list of sockets and found which socket's .fileno() matched x. You could
        probably be clever and use a dict() with the filenos as key and socket as
        value
        '''

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2020-02-04
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多