简体   繁体   English

Python套接字编程recv

[英]Python socket programming recv

I've started with python today and i'm looking into socket programming atm. 我今天开始使用python,我正在研究socket编程。 Ive connected to an ip and port with a socket instance and im trying to read the next 1024 bytes on the socket with the recv method. 我已经连接到带有套接字实例的ip和端口,我试图用recv方法读取套接字上的下一个1024字节。 When im trying to store it in a variable, I get an error telling me that the socket times out, tried increasing it but didn't help. 当我试图将它存储在变量中时,我收到一个错误,告诉我套接字超时,尝试增加它但没有帮助。

Code: 码:

>>> import socket
>>> socket.setdefaulttimeout(2)
>>> s = socket.socket()
>>> s.connect(("192.168.95.148",21))

error: 错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.timeout: timed out

You set a timeout in socket.setdefaulttimeout(2) , therefore it raises an exception. 您在socket.setdefaulttimeout(2)设置了超时,因此它引发了异常。

Try this instead: 试试这个:

import socket
s = socket.socket()
socket.setdefaulttimeout(10) #set timeout for ten seconds (can be whatever you want)
try:
    s.connect(("192.168.95.148",21))
except Exception as e:
    print("An error occured:\n{}".format(e))

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

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