在Python中使用套接字:仅在使用IDLE时才能正确运行。(Using Sockets in Python: Only runs correctly when using IDLE. Does no

编程入门 行业动态 更新时间:2024-10-15 12:33:19
在Python中使用套接字:仅在使用IDLE时才能正确运行。(Using Sockets in Python: Only runs correctly when using IDLE. Does not run correctly in CMD)

我试图执行一个简单的套接字通信脚本,并尝试使用命令提示符运行脚本时遇到一些困难。 我正在使用Windows 7 64位与Python 2.7.5 AMD 64。

当我使用IDLE执行它时,脚本工作正常,但是当使用命令提示符时,它只是坐在那里歪着它的拇指。

这是我用来尝试解决问题的代码:

from socket import * print "Hello World" tcp_ip = '192.168.1.241' tcp_port = 2001 buffer_size = 11 plc_socket = socket(AF_INET, SOCK_STREAM) plc_socket.bind((tcp_ip, tcp_port)) plc_socket.listen(1) socket_conn, socket_addr = plc_socket.accept() print "Connection Accepted" socket_conn.shutdown(1) plc_socket.close()

当我使用命令行运行代码时,我确实得到了“Hello World”,它在SOCKET_CONN,SOCKET_ADDR行之前完成所有操作,然后就在那里。

我已经做了以下事情:

将我的Windows路径信息设置为指向Python安装目录。 卸载了我机器上的任何其他Python版本。 在其他机器(相同的构建)上尝试脚本以查看它是否有效。 (确实如此) 验证我从命令行运行正确版本的Python。 困惑地抓住了我的脑袋。

任何有关此事的智慧都将受到高度赞赏。

I am trying to execute a simple socket communication script and having some difficulties when trying to run the script using the command prompt. I am using Windows 7 64-bit with Python 2.7.5 AMD 64.

The script works fine when I execute it with IDLE, however when use the command prompt it just sits there twiddling it's thumbs.

Here is the code that I am using to try to troubleshoot the problem:

from socket import * print "Hello World" tcp_ip = '192.168.1.241' tcp_port = 2001 buffer_size = 11 plc_socket = socket(AF_INET, SOCK_STREAM) plc_socket.bind((tcp_ip, tcp_port)) plc_socket.listen(1) socket_conn, socket_addr = plc_socket.accept() print "Connection Accepted" socket_conn.shutdown(1) plc_socket.close()

When I run the code by using the command line, I do get the "Hello World" and it does everything before the SOCKET_CONN, SOCKET_ADDR line and then just sits there.

I have already done the following:

Set my windows path information to point to the Python installation directory. Uninstalled any other versions of Python that were on my machine. Tried script on other machines (identical build) to see if it works. (it does) Verified that I am running the correct version of Python from the command line. Scratched my head in confusion.

Any wisdom on the matter would be greatly appreciated.

最满意答案

问题已经解决了。

事实证明,当脚本由命令行运行或直接执行时,本地网络防火墙阻止了TCP / IP连接。

我关闭了防火墙,脚本运行完美。

The problem has been solved.

It turns out that a local network firewall was blocking the TCP/IP connection when the script was being run by the command line or executed directly.

I turned off the firewall and the script worked perfectly.

更多推荐

本文发布于:2023-08-06 13:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时才   正确   Python   IDLE   Sockets

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!