【发布时间】:2014-05-07 02:38:44
【问题描述】:
所以现在为了收到您的消息,您需要收到一个
我的老师的指令是(主要)“修改循环,使其仅侦听键盘输入,然后将其发送到服务器。” 我做了其余的,但不明白这一点,...帮助?
import socket
import select
import sys
import threading
'''
Purpose: Driver
parameters: none
returns: none
'''
def main():
host = 'localhost'
port = 5000
size = 1024
#open a socket to the client.
try:
clientSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSock.connect((host,port))
#exit on error
except socket.error, (value,message):
if clientSock :
clientSock.close()
print "Could not make connection: " + message
sys.exit(1)
thread1 = ClientThread()
thread1.start()
while True:
#wait for keyboard input
line = raw_input()
#send the input to the server unless its only a newline
if line != "\n":
clientSock.send(line)
#wait to get something from the server and print it
data = clientSock.recv(size)
print data
class ClientThread(threading.Thread):
'''
Purpose: the constructor
parameters: the already created and connected client socket
returns: none
'''
def __init__(self, clientSocket):
super(ClientThread, self).__init__()
self.clientSocket = clientSocket
self.stopped = False
def run(self):
while not self.stopped:
self.data = self.clientSocket.recv(1024)
print self.data
main()
【问题讨论】:
-
我能提供更多信息不应该那么复杂吗?
-
你能描述一下这个程序的假定行为吗?怎么了?
标签: python multithreading sockets python-2.7 python-3.x