网络编程之TCP服务器与客户端(《Core Python Programming》)

书中代码是基于UNIX的,刚开始忽略了这一点,程序总报错

  File "D:/Python/tsTclnt3.py", line 15, in <module>

    tcpCliSock.send(data)

TypeError: a bytes-like object is required, not 'str'

在windows下运行要注意编码问题

服务器端书中代码为:


from socket import *
from time import ctime


HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)


tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)


while True:
    print('waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    print('...connected from:',addr)


    while True:
        data = tcpCliSock.recv(BUFSIZ)
        if not data:
            break
        tcpCliSock.send('[%s] %s' %(bytes(ctime(),'utf-8'),data))
    tcpCliSock.close()
tcpSerSock.close()

客户端书中代码为:

from socket import *

HOST = 'localhost'
PORT= 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
    data = input('>')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print(data)

tcpCliSock.close()

更改编码问题之后代码为:

服务器端:

from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
    print('waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    print('...connected from:',addr)

    while True:
        data = tcpCliSock.recv(BUFSIZ).decode() #接受的数据要解码
        if not data:
            break
        tcpCliSock.send(('[%s] %s' %(ctime(),data)).encode())  #发送的数据要编码
    tcpCliSock.close()
tcpSerSock.close()

客户端:

from socket import *

HOST = 'localhost'
PORT= 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)

while True:
    data = input('>')
    if not data:
        break
    tcpCliSock.send(data.encode()) #发送的数据要编码
    data = tcpCliSock.recv(BUFSIZ).decode() #接受的数据要解码
    if not data:
        break
    print(data)

tcpCliSock.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Core PYTHON Programming, 2nd Edition (pdf, Python 2.5) Python is an agile, robust, expressive, fully object-oriented, extensible, and scalable programming language. It combines the power of compiled languages with the simplicity and rapid development of scripting languages. In Core Python Programming, Second Edition, leading Python developer and trainer Wesley Chun helps you learn Python quickly and comprehensively so that you can immediately succeed with any Python project. Using practical code examples, Chun introduces all the fundamentals of Python programming: syntax, objects and memory management, data types, operators, files and I/O, functions, generators, error handling and exceptions, loops, iterators, functional programming, object-oriented programming and more. After you learn the core fundamentals of Python, he shows you what you can do with your new skills, delving into advanced topics, such as regular expressions, networking programming with sockets, multithreading, GUI development, Web/CGI programming and extending Python in C. This edition reflects major enhancements in the Python 2.x series, including 2.5 as well as capabilities set for future versions. It contains new chapters on database and Internet client programming, plus coverage of many new topics, including new-style classes, Java and Jython, Microsoft Office (Win32 COM Client) programming, and much more. Learn professional Python style, best practices, and good programming habits Gain a deep understanding of Python’s objects and memory model as well as its OOP features, including those found in Python’s new-style classes Build more effective Web, CGI, Internet, and network and other client/server applications Learn how to develop your own GUI applications using Tkinter and other toolkits available for Python Improve the performance of your Python applications by writing extensions in C and other languages, or enhance I/O-bound applications by using multithreading Learn about Python’s database API and how to use a variety of database systems with Python, including MySQL, Postgres, and SQLite Core Python Programming delivers Systematic, expert coverage of Python’s core features Powerful insights for developing complex applications Easy-to-use tables and charts detailing Python modules, operators, functions, and methods Dozens of professional-quality code examples, from quick snippets to full-fledged applications The Complete Developer’s Guide to Python—Fully Updated for Python 2.5 New to Python? The definitive guide to Python development for experienced programmers Covers core language features thoroughly, including those found in the latest Python releases Learn advanced topics such as regular expressions, networking, multithreading, GUI, and Web/CGI Includes brand-new chapters on database, Internet, Jython, and COM Client programming Presents hundreds of code samples and practical exercises to strengthen your Python skills Book Details Paperback: 1136 pages Publisher: Prentice Hall; 2nd Edition (September 2006) Language English ISBN-10: 0132269937 ISBN-13: 978-0132269933 File Size: 55.9 MiB epub 格式 http://download.csdn.net/source/3560240
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值