Python保持socket活? [英] Python keeping socket alive?

查看:710
本文介绍了Python保持socket活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在使用一个python项目使用套接字。基本上我想循环一个连接到主机的用户输入。这是我想要的:

Hey I'm working on a python project using sockets. Basically I want to loop a connection to a host for user input. Here is what I'm trying:


while True:
    sock.connect((host, port))
    inputstring = " > "
    userInput = raw_input(inputstring)
    sock.send(userInput + '\r\n\r\n')
    recvdata = sock.recv(socksize)
    print(recvdata)

但是当我循环套接字并尝试连接到localhost在端口80我得到一个错误说传输端点仍然连接或类似的。

But when I loop the socket and try connecting to localhost on port 80 I get an error saying the transport endpoint is still connected or something like that. How can I eliminate that problem?

推荐答案

调用 connect code> while 循环。您只需连接一次。

Call connect outside the while loop. You only need to connect once.

这篇关于Python保持socket活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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