首页 > 解决方案 > 使用 Socket python 发送数据

问题描述

我正在使用python中的套接字进行连接。一个简单的机制是客户端输入一封电子邮件并被服务器接收,然后服务器将处理(拆分电子邮件)并将拆分的电子邮件发送回客户端

这是客户端代码,客户端必须输入一些电子邮件并由服务器接收

import socket

PORT = 1234
IPADDRESS = '127.0.0.1'

def inputEmailServer(IPADDRESS, PORT):
    s = socket.socket()
    s.connect((IPADDRESS, PORT))
    while True:
        inputEmail = input("Please enter your email : ")
        s.send(inputEmail.encode('utf-8'))
        isi = s.recv(1024)
        print(isi)

while True:
    inputEmailServer(IPADDRESS, PORT)

这是服务器代码,服务器必须处理客户端发送的电子邮件

import socket
import re
import rsa

PORT = 1234
IPADDRESS = '127.0.0.1'

def emailEncrypt(email):
    publicKey, privateKey = rsa.newkeys(512)
    encEmail = rsa.encrypt(email.encode(), publicKey)
    decEmail = rsa.decrypt(encEmail, privateKey).decode()

    print('Email : ', email)
    print('Encrypted email : ', encEmail)

def emailSplit(email):
    username = email.split('@')[0]
    domain = email.split('@')[1]
    domain_name = domain.split('.')[0]
    domain_type = domain.split('.')[1]

    print('Username : ', username)
    print('Domain   : ', domain_name)
    print('Type     : ', domain_type)

def emailInput(inputEmail):
    regex = '^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$'
    
    if len(inputEmail) == 0:
        print("Please enter your email again")
    elif inputEmail.isdigit():
        print("Please enter string")
    elif not re.search(regex, inputEmail):
        print("Invalid email")
    else:
        emailSplit(inputEmail)

def inputServer(IPADDRESS, PORT):
    s = socket.socket()
    s.bind((IPADDRESS, PORT))
    s.listen(5)
    c, addr = s.accept()
    print ('Socket Up and running with a connection from',addr)
    while True:
        rcvdData = c.recv(1024).decode('utf-8')
        output = emailInput(rcvdData)
        c.sendall(output)

while True:
    inputServer(IPADDRESS, PORT)

但结果只是

b''

结果一定是这样的

Username :  name
Domain   :  gmail
Type     :  com

你能帮我解决这个问题吗?谢谢!

标签: pythonsockets

解决方案


您的代码有错误。你想念你的回报。下面是固定代码。

代码标记

import json  # TODO: import json
import socket
import re
import rsa

PORT = 1234
IPADDRESS = '127.0.0.1'


def emailEncrypt(email):
    publicKey, privateKey = rsa.newkeys(512)
    encEmail = rsa.encrypt(email.encode(), publicKey)
    decEmail = rsa.decrypt(encEmail, privateKey).decode()

    print('Email : ', email)
    print('Encrypted email : ', encEmail)


def emailSplit(email):
    username = email.split('@')[0]
    domain = email.split('@')[1]
    domain_name = domain.split('.')[0]
    domain_type = domain.split('.')[1]

    print('Username : ', username)
    print('Domain   : ', domain_name)
    print('Type     : ', domain_type)
    # TODO: need return data
    return json.dumps({"Username": username, "Domain": domain_name, "Type": domain_type}).encode('utf-8')


def emailInput(inputEmail):
    regex = '^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$'

    if len(inputEmail) == 0:
        print("Please enter your email again")
    elif inputEmail.isdigit():
        print("Please enter string")
    elif not re.search(regex, inputEmail):
        print("Invalid email")
    else:
        # TODO: need return data
        # emailSplit(inputEmail)
        return emailSplit(inputEmail)


def inputServer(IPADDRESS, PORT):
    s = socket.socket()
    s.bind((IPADDRESS, PORT))
    s.listen(5)
    c, addr = s.accept()
    print('Socket Up and running with a connection from', addr)
    while True:
        rcvdData = c.recv(1024).decode('utf-8')
        print("rcvdData: %s" % rcvdData)  # TODO: debug output
        output = emailInput(rcvdData)
        print("output: %s, type: %s" % (output, type(output)))  # TODO: debug output
        c.sendall(output)


while True:
    inputServer(IPADDRESS, PORT)

推荐阅读