2台のマシンがあり、1台はクライアントとして、もう1台はサーバーとしてセットアップされています。クライアントは、サーバーからコマンドを受信して応答するように設定されています。私は知っています...後方。Putty を使用してみましたが、接続が拒否され続け、クライアント/サーバー モデルが逆になっていることがわかりました。次のコードは機能しているようですが、データがクライアントに送信されたことを確認できません。ありがとう。
私のサーバープログラムは次のとおりです。
# Sets up server on PC that it is run on.
# Attempting to send messages from Server to Client
# to run in windows open command prompt and from directory that the
# file is located in and run it. Or rund from IDLE.
# Python 3.1
import socketserver
import socket
from threading import *
import time
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
message = bytes('hello world from server'+'\n', 'utf-8')
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
print("Printed by Server - text From Client")
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
self.request.sendall(message)
# Added from website - http://stackoverflow.com/questions/21233340/sending-string-via-socket-python
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
print('Client sent:', self.sock.recv(1024).decode())
self.sock.send(b'Oi you sent something to me')
if __name__ == "__main__":
HOST, PORT = "127.0.0.1", 5005 #HOST had to be IP of server
# and PORT has to be available?
# Create the server, binding to localhost on port XXXX
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
# Added from website - http://stackoverflow.com/questions/21233340/sending-string-via-socket-python
serversocket.listen(5)
print ('server started and listening')
while 1:
clientsocket, address = serversocket.accept()
client(clientsocket, address)
エンド サーバー:
クライアント:
# TCPClient - used to sen`enter code here`d data via TCP to Server
# Python 3.1
import socket
import sys
#HOST, PORT = "192.168.0.27", 85 #HOST port needs to be port of server (other machine)
# data = " ".join(sys.argv[1:])
#data = input("Enter a message you would like to send: ")
answer = "Y"
#Function serves data to Host and Port
def Serve(HOST, PORT, DATA):
"""Send and Receive Data To Server"""
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
finally:
sock.close()
print("Sent: {}".format(data), " HOST: ", HOST, " PORT: ", PORT)
print("Received: {}".format(received))
while answer == "Y":
data = input("Enter a message you would like to send: ")
Serve("127.0.0.1", 5005, data)
answer = input("Would you like to send another message? Y/N:")
answer = answer.upper()
print("Good by")
エンド クライアント:
IDLE で実行: サーバー側
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
127.0.0.1 wrote:
b'hello'
Printed by Server - text From Client
127.0.0.1 wrote:
b'hello'
Printed by Server - text From Client
127.0.0.1 wrote:
b'hello'
Printed by Server - text From Client
127.0.0.1 wrote:
b'hello again'
Printed by Server - text From Client
クライアント:
Enter a message you would like to send: hello
Sent: hello HOST: 127.0.0.1 PORT: 5005
Received: HELLO
Would you like to send another message? Y/N:y
Enter a message you would like to send: hello again
Sent: hello again HOST: 127.0.0.1 PORT: 5005
Received: HELLO AGAIN
Would you like to send another message? Y/N:n
Good by