Python服务器发送数据不工作 [英] Python Server send data not working

查看:118
本文介绍了Python服务器发送数据不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python中的服务器,我面临的问题是客户端无法从服务器检索发送的数据。

I am currently working on a server in Python, the problem I am facing is the client could not retrieve the sent data from server.

服务器的代码是:

import sys
import socket
from threading import Thread

allClients=[]

class Client(Thread):

    def __init__(self,clientSocket):
                Thread.__init__(self)
                self.sockfd = clientSocket #socket client
                self.name = ""
                self.nickName = ""

    def newClientConnect(self):

      allClients.append(self.sockfd)
      while True:
            while True:
                try:
                    rm= self.sockfd.recv(1024)
                    print rm

                    try:
                        self.sockfd.sendall("\n Test text to check send.")
                        print "Data send successfull"
                        break


                    except socket.error, e:
                        print "Could not send data"

                    break

                except ValueError:
                       self.sockfd.send("\n Could not connect properly")


    def run(self):
                self.newClientConnect()
                self.sockfd.close() 
                while True:
                        buff = self.sockfd.recv(1024)

                        if buff.strip() == 'quit':
                            self.sockfd.close()
                            break # Exit when break
                        else:
                            self.sendAll(buff)
#Main
if __name__ == "__main__":

    #Server Connection to socket:
    IP = '127.0.0.1'
    PORT = 80
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR,1)

    print ("Server Started")
    try:
        serversocket.bind(('',5000))
    except ValueError,e:
        print e
    serversocket.listen(5)

while True:
        (clientSocket, address) = serversocket.accept()
        print 'New connection from ', address
        ct = Client(clientSocket)
        ct.start()

__all__ = ['allClients','Client']

#-- 

与客户连接的是:

import socket

HOST = '192.168.1.4'    # The remote host
PORT = 5000              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

data = s.recv(1024)
s.close()
print 'Received', data#repr(data)

需要一个快速解决方案....
谢谢,

In need of a quick solution.... Thanks,

推荐答案

我测试了你的代码,当我注释掉时

I tested out your code, and when I commented out

rm= self.sockfd.recv(1024)
print rm

工作正常。基本上服务器停在那里等待一个从来没有来的消息。如果它仍然不能为你工作,可能有两个问题。或者你有一个防火墙阻塞连接不知何故,或者你有旧的服务器在后台运行从以前的尝试,实际上没有被杀死。检查您的进程,如果pythonw.exe或等效的运行时,它不应该,并杀死它。

it worked fine. Basically the server stopped there to wait for a message that never came. If it still does not work for you, there might be two problems. Either you have a firewall that blocks the connection somehow, or you have old servers running in the background from previous tries that actually wasn't killed. Check your processes if pythonw.exe or equivalent is running when it shouldn't be, and kill it.

这篇关于Python服务器发送数据不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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