python - 使用 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
你能帮我解决这个问题吗?谢谢!
解决方案
您的代码有错误。你想念你的回报。下面是固定代码。
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)
推荐阅读
- python - 如何在 matplotlib 中组合多个密度图?
- vue.js - VueJS 称自己为渐进式框架是什么意思?
- javascript - 如何在伪造查看器上推送文本?
- javascript - 如何将不和谐消息发送到另一个频道?(不和谐 JS)
- python - 如何通过 pandas 库 sqlalchemy 执行 SQL while 循环。不要关闭这个问题,因为我没有找到任何关于这个的答案
- npm - 在 redhat 8 上全局安装 pm2
- python - 有没有办法在 Pandas 中删除具有特定条件的重复行?
- node.js - Fetch API 适用于 POSTMAN 和 express 服务器,但不适用于 React js
- r - 如何使用 dplyr mutate 引用 ifelse 中的其他列
- html - 小烦恼:两人之间的神秘不间断空间