简体   繁体   English

Python socket.connect()错误出

[英]Python socket.connect() errors out

Python version 2.7.3 Python版本2.7.3

Python code, interactive mode Python代码,交互模式

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

Errors out 错误出

>>> s.connect(("192.168.95.148",21))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.timeout: timed out

Syntax looks correct. 语法看起来正确。 I even tried with another IP address that is pingable and same error. 我什至尝试使用可ping并且相同错误的另一个IP地址。

Thanks! 谢谢!

I am looking at the port you are using (21) and it's the FTP port. 我正在查看您正在使用的端口(21),它是FTP端口。

https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

I don't see any errors in your code per se, but here some things I am worried about: 我自己的代码本身看不到任何错误,但是这里有些我担心的事情:

a) A lot of places lock down and turn off FTP , telnet , etc. ports that are historically used, but in today's security consious world, we use scp and ssh (instead of ftp and ssh ). a)很多地方会锁定并关闭历史上使用的FTPtelnet等端口,但是在当今的安全环境中,我们使用scpssh (而不是ftpssh )。 You might talk to your system administrators and see if that service is even turned on, or let alone your filewall even allows that port to go through. 您可以与系统管理员联系,以查看该服务是否已打开,或者更不用说您的文件墙甚至允许该端口通过。

b) I am not used to seeing the default parameters, but I assume you want b)我不习惯看到默认参数,但是我想你想

socket(socket.AF_INET, socket.SOCK_STREAM) 套接字(socket.AF_INET,socket.SOCK_STREAM)

I have Python 2.6, and that is the defaults for that (which I assume is the same for 2.7). 我有Python 2.6,这是默认设置(我认为与2.7相同)。

c) I would try this on different ports to see if it is a port problem. c)我将在其他端口上尝试此操作,以查看是否是端口问题。 Of course, the early numbered ports are special "well-known ports" that normal users can't use. 当然,早期编号的端口是普通用户无法使用的特殊“知名端口”。 I usually use port 9711 or 8888. 我通常使用端口9711或8888。

d) I would also try it without the timeout and see if that works. d)我也可以尝试不超时的方法,看看是否可行。

I hope this helps! 我希望这有帮助!

这不是您问题的直接答案,但是如果您要进行FTP传输,我可以建议使用标准ftplib模块吗?

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

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