- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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.
>>>
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()
最佳答案
好吧,我知道你 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
'''
关于带套接字和 sys.stdin 的 Python 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766398/
我正在编写一个 python 程序,它将所有输入都大写(替代非工作 tr '[:lowers:]' '[:upper:]')。语言环境是 ru_RU.UTF-8,我使用 PYTHONIOENCODIN
我收到错误;MVC Microsoft JScript 运行时错误:Sys.ArgumentTypeException:“Sys._Application”类型的对象无法转换为“Sys._Applic
尝试使用 IDLE 在 Python 3.7.4 版本中安装 sys 包时,出现以下错误: 输入:C:\Users\UserName\Downloads>pip install sys 输出: 采集系
我对 Python 还很陌生,所以我还在学习这门语言。我遇到的一件事是重新分配 sys.stdout 以更改打印的默认输出。所以我写了这个作为测试: import sys sys.stdout = o
我应该选择 sys.exc_info()在 sys.last_value和 friend (sys.last_type,sys.last_traceback)? 最佳答案 查看sys.last_val
我的 shell 脚本中出现奇怪的错误。使用这个: $find /sys/class/ -name temp -exec cat '{}' ';' 我得到输出 77000 find: `/sys/ke
我只想从 sys 库导入 argv 和可执行函数。 所以我使用 from 和 import 来指定这些函数,如下所示:- from sys import argv from sys import ex
我想找到调用我的程序的命令行参数,即 sys.argv,但我想在 Python 使 sys.argv 可用之前执行此操作.这是因为我在 usercustomize.py 中运行代码,该代码由 site
sys和os.sys在python中有什么区别?我见过很多项目在导入os时使用sys。当我尝试 dir(sys) 和 dir(os.sys) 时,它们的功能相同,输出也相同。 我经常看到像这样使用 s
create table T ( ID number, COL1 SYS.XMLTYPE )XMLType COLUMN COL1 STORE AS CLOB; select obj#,col
下面的 SQL 似乎可以工作,但我想知道是否有更好的方法来编写它。我正在尝试选择具有特定名称的所有 View 和表。如果找到匹配项,则应该只有一个返回值。 SELECT DISTINCT name F
在我的客户端(使用 LWJGL)中,我使用以下代码: private static long getTime() { return (Sys.getTime() * 1000) / Sys.g
我的老板让我看一些旧代码,其中所有内容都被发送到 stderr。我知道 stderr 应该有警告和错误,但他们什么时候才真正应该转到 stdout? 此程序是一项服务。它发送到 stderr 的一些消
使用 importlib,“Meta Path Finder”(通过遍历 sys.meta_path 找到)和“Path Entry Finder”(通过遍历 sys.path_hooks 找到)有什
我正在运行这个命令来安装 os_sys 包: pip3 install os_sys 但收到此错误: ERROR: os-sys has an invalid wheel, could not rea
如果我查询sys.dm_database_encryption_keys,它返回的encryption_state为3(加密),percent_complete 0。如果我查询 sys.databas
有人可以向我解释一下这些不同的环境函数具体有什么作用吗?即哪个返回什么帧?阅读文档后我完全困惑了(http://stat.ethz.ch/R-manual/R-patched/library/base
查看其他 stackoverflow 帖子后,我似乎无法解决这个重定向问题。我想做的是抑制 stdout 和 stderr,然后在捕获错误后恢复它们。抑制效果很好,但恢复它们只成功了一半。 如果我尝试
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 3 个月前。 我写了这段代码: #!/usr/bin/env python import sys i
我正在创建一个名为 Qt ( Github link ) 的模块,我在其中为另一个模块起别名(例如 PyQt4),这样当我导入 Qt 我实际上正在导入 PyQt4: from Qt import Qt
我是一名优秀的程序员,十分优秀!