Python套接字模块。服务器未发送回收到的数据

时间:2019-02-03 19:26:31

标签: python-3.x

随后运行服务器和客户端时,它会打印出已建立连接,但我从客户端发送的数据没有通过。

客户

import socket

def connect():

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = "127.0.0.1"
    port = 7878
    s.connect((host, port))

    ter = "terminate"
    data = raw_input("-> ")
    while ter not in data:
        if ter in data:
            s.close()
            break
        else:
            rec = s.recv(1024)
            print("Recieved:",rec)
        data = input("-> ")
connect()

服务器

import socket

def connect():
    print("Server started")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = "127.0.0.1"
    port = 7878
    s.bind((host, port))

    s.listen(1)
    c, addr = s.accept()
    print("Got connection from", addr)

    while True:
        data = c.recv(1024)
        if len(str(data)) < 0:
            c.send("Got nothing send again")
        else:
            data = data.upper()
            c.send(data)

connect()

0 个答案:

没有答案