Python simple way to implement TCP packets to send hex data

Source: Internet
Author: User
The example in this paper describes how Python simply implements TCP packets to send hexadecimal data. Share to everyone for your reference, as follows:

For example: 0x12, 0x34 can be directly spelled as "\x12\x34".

Sample client code:

#-*-encoding:utf-8-*-import jsonimport socketimport sysimport binasciireload (SYS) sys.setdefaultencoding (' Utf-8 ') if __name__== "__main__":    s = Socket.socket (socket.af_inet,socket. SOCK_STREAM)    S.connect (("Your_host_name", Your_port)    s.send ("\xab\xcd\x34\x12\x1f\x00_some_orther_data ") # preceded by hexadecimal data, followed by strings and other body    print s.recv (1024x768)    s.close ()

More readers interested in Python related content can view this site topic: "Python function Tips Summary", "Python string manipulation Tips", "Python Introductory and Advanced Classic tutorial" and "Python file and directory Operations Tips Summary"

I hope this article is helpful for Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.