简体   繁体   English

Python:客户端脚本中有多个套接字?

[英]Python: multiple sockets in a client script?

I had this excercise for a class which wanted me to develop a client socket script (in Python 2.x) that connected to a server, received a string and sent back the res variable. 我在一个类的练习中工作,该类想让我开发一个客户端套接字脚本(在Python 2.x中),该脚本连接到服务器,接收字符串并发回res变量。 The professor straight up told us to look up in the internet about similar scripts and after a relevant search i came up with a base script that worked perfectly for the excercise: 这位教授直截了当地告诉我们要在互联网上查找类似的脚本,在进行了相关搜索之后,我想出了一个基本的脚本,可以很好地锻炼该脚本:

from socket import *
import string, sys, select

HOST = '...'
PORT = 5555

s = socket(AF_INET,SOCK_STREAM)
s.connect((HOST,PORT))

while 1:

 socket_list = [sys.stdin, s]
 read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
 for sock in read_sockets:
        if sock == s:
            data = sock.recv(4096)
            if not data : sys.exit()
            else:
              dec = map(ord,data)
              res = max(dec) + min(dec)
              print data+'\n(^min_max value in ascii: '+str(res)+')'
        else :
            msg = sys.stdin.readline()
            s.send(msg)

However, since I'm really new to Python, and the script is based from a random page on the web, I can't fully understand the following: 但是,由于我是Python的新手,并且该脚本基于网络上的随机页面,因此我无法完全理解以下内容:

socket_list = [sys.stdin, s]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

for sock in read_sockets:

What is happening in these lines? 这些行中发生了什么? I do understand why I use the two sockets, but the second line confuses me. 我确实理解为什么使用两个插槽,但是第二行使我感到困惑。 Any help? 有什么帮助吗?

PS: I tried removing that line and changing the loop-statement with PS:我尝试删除该行并使用以下命令更改循环语句

for sock in socket_list

but when i wrote back at the server it didn't accept the right answer 但是当我写回服务器时,它没有接受正确的答案

According to what the documentation says about select [1], it returns the sockets ready to read/write. 根据文档中关于select [1]的描述,它返回准备读取/写入的套接字。 If you do for sock in socket_list you are just picking one socket and trying to read even if that socket is not ready (means it doesn't have any data). 如果您for sock in socket_list做操作for sock in socket_list那么您只是在选择一个套接字并尝试读取,即使该套接字尚未准备就绪(意味着它没有任何数据)。

You could try making the steps one by one: 您可以尝试一步一步地进行以下操作:

while 1:

    socket_list = [sys.stdin, s]
    #read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
    #for sock in read_sockets:
    #if sock == s:

    # read from stdin
    msg = sys.stdin.readline()

    # send string to the server
    s.send(msg)

    # Read from server (it should have data now)
    data = sock.recv(4096)
    if not data:
        sys.exit()

    dec = map(ord,data)
    res = max(dec) + min(dec)
    print data+'\n(^min_max value in ascii: '+str(res)+')'

[1] https://docs.python.org/2/library/select.html#select.select [1] https://docs.python.org/2/library/select.html#select.select

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

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