宣布与客户端的服务器连接自动TCP / IP Python

时间:2018-02-16 22:24:56

标签: python tcp server client

我目前正在尝试宣布客户端最初何时连接到服务器并打印出简单的消息,例如"您已连接"。这是我的第一个使用python的程序,因此我确信他们可以通过更简单的方式完成我已完成的工作,并且我可以接受提示

服务器成功响应命令" HELO" " REQIP" " REQDATE" " REQTIME" " ECHO"和" BYE"

这是我的tcp服务器

import socket
import time
host = '127.0.0.1'
port = 8008

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
c, addr = s.accept()
def Main():
    #print("Server Listening")
    while True:
            data = c.recv(1024).decode('utf-8')
            if not data:
                    break
            if data =='HELO':
                    Hello(data)
            elif data == 'REQTIME':
                    REQTIME(data)
            elif data == 'REQDATE':
                    REQDATE(data)
            elif data == 'REQIP':
                    IP(data)
            elif data =='BYE':
                    BYE()
            elif data[:4] == 'ECHO': 
                    ECHO(data)
            else:
                    message = ("Invalid command")
                    c.send(message.encode('utf-8'))

def Hello(data):
    if data !='HELO':
            message = ("Server: 510 Sorry i can not service your request. \n Connection Closed")
            c.send(message.encode('utf-8'))
            c.close()

    message = ("Server: 210 Hello " + host + " pleased to meet you.")
    c.send(message.encode('utf-8'))
    Main()

def REQTIME(data):
    if data !='REQTIME':
            message = ("Server: 520 Sorry, time is not available right now")
            c.send(message.encode('utf-8'))

    message = ("Server: 220 " + (time.strftime('%H:%M:%S')))
    c.send(message.encode('utf-8'))
    Main()

def REQDATE(data):
    if data !='REQDATE':
            message =("Server: 530 Sorry, date is not available right now")
            c.send(message.encode('utf-8'))

    message = ("Server: 230 " +  (time.strftime("%Y/%m/%d")))
    c.send(message.encode('utf-8'))
    Main()

def ECHO(data):
    if data[:4] !='ECHO':
            message =("Server: 5350 Cant seem to echo")
            c.send(message.encode('utf-8'))

    text = ("Server: 240 " )
    message = text + data[4:]
    c.send(message.encode('utf-8'))
    Main()

def IP(data):
    if data !='REQIP':
            message = ("Server: 550 Sorry i can not get your IP Address.")

    message =("Server: 250 " + host)
    c.send(message.encode('utf-8'))
    Main()


def BYE():           
    message =("Server: 600 See Ya Later!")
    c.send(message.encode('utf-8'))

if __name__ == '__main__':
    Main()

我的tcp客户端

import socket
def Main():
    host = '127.0.0.1'
    port = 8008
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    message = input("->")
    myint = 0
    while message != 'EXIT':
            s.send(message.encode('utf-8'))
            data = s.recv(1024).decode('utf-8')
            print(data)
            if (message == 'BYE'):
                    break
            message = input("->")
    s.close()


if __name__ == '__main__':
    Main()

1 个答案:

答案 0 :(得分:0)

在你的服务器中,只需在“accept()”之后添加一个c.send(“你的消息”)。

在你的客户端中,在s.connect((host,port))之后添加一个recv()和print()。

但我不喜欢服务器部分中程序的启动。当你启动服务器时,它正在等待一个带有“listen”的新连接,并且在连接之后它将进入main函数。也许你可以改变它并将你的全局部分放在主要功能中。