简体   繁体   English

Python套接字(简单服务器脚本)

[英]Python Socket (Simple server script)

I have this simple server script . 我有这个简单的服务器脚本 My objective for this script is : 此脚本的目标是:

  1. Wait for a client connection 等待客户端连接
  2. Receive massages from client until the client disconnect 从客户那里收到消息,直到客户断开连接
  3. Once client disconnect, wait for another client to connect 客户端断开连接后,等待另一个客户端连接
  4. Receive massages until the client disconnect 接受按摩直到客户断开
  5. Repeat... 重复...

import socket

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(("",5000))
server_socket.listen(5)
print "Awaiting Client connection"
client_socket, address =server_socket.accept()
print "Connection established.. with ",address
while True:
    data=client_socket.recv(512)
    if not data:
        client_socket.close()
        print "Client disconnected, Awaiting new connections..."
        client_socket, address =server_socket.accept()
        print "Connection from ",address
    else:
        print "RECIEVED:",data

My Question is even though the script seems to be working when i test it on a pair of pc, i noticed that after it received the connection from the client, that is line no 7 我的问题是,即使我在两台PC上测试脚本时脚本似乎也可以正常工作,但我注意到在收到客户端的连接后,这是line no 7

print "Connection established.. with ",address

the python shell window seems unresponsive (become not responding if i try to move the shell window) until the client send any message. python shell窗口似乎无响应(如果我尝试移动shell窗口,则无响应),直到客户端发送任何消息为止。

As far as i understand, if there is no incoming message from client, client_socket.recv(512) will just wait for the data from the client. 据我了解,如果没有来自客户端的传入消息, client_socket.recv(512)将只等待来自客户端的数据。

But why it became unresponsive? 但是为什么它变得不响应? To make things clearer, 为了使事情更清楚

-the script works just fine ( it receive data and print it out from screen & wait for the new connection if client disconnect) -脚本工作正常(它接收数据并从屏幕上打印出来,如果客户端断开连接,则等待新的连接)

-the cursor in console windows stop blinking -控制台窗口中的光标停止闪烁

-when i try to move the console window around , it become unresponsive and windows give me a message "this program has stopped responding" -当我尝试移动控制台窗口时,它变得无响应,并且窗口向我显示一条消息“此程序已停止响应”

Basically, you script blocks on the accept call that is present after this line : 基本上,您可以在此行之后的accept调用中编写脚本块:

print "Client disconnected, Awaiting new connections..."

The accept call will return only when a particular client has attempted to connect to server. 仅当特定客户端尝试连接到服务器时, accept调用才会返回。 That is when your script will continue execution to the next line. 那是您的脚本将继续执行到下一行的时间。 This is the reason why you would see a message like This program is not responding in Windows. 这就是为什么您会看到类似此程序在Windows中没有响应的消息的原因。

You could consider the use of non-blocking socket I/O approach in order to ensure that your script is responsive. 您可以考虑使用非阻塞套接字I / O方法,以确保脚本能够响应。

Refer to this link for description of blocking and non-blocking calls. 请参考链接以获取有关阻止和非阻止调用的描述。 Also you can refer to this question to understand how to implement Non-blocking sockets in Python - and of course there are plenty of web resources too. 您也可以参考问题,以了解如何在Python中实现非阻塞套接字-当然,还有很多Web资源。

Hope this helps 希望这可以帮助

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

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