Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:38:07+00:00 2026-05-26T17:38:07+00:00

I am currently looking into socket programming in python and I am experiencing some

  • 0

I am currently looking into socket programming in python and I am experiencing some strange behavior with socket.htons() … it appears to be flipping the bytes on every call.

I am implementing a simple ping script, as far as I am aware network byte order is big endian and my systems byte order is little endian .

If I use htons on my 16 bit checksum wireshark reports that it is incorrect , however if I just pack the checksum into a struct without using htons wireshark confirms it is correct.

This is what wireshark has captured when using htons

Checksum: 0xece4 [incorrect, should be 0xe4ec]

And here is a quick example…

>>> z = 0xFF00
>>> print z
65280
>>> z = socket.htons(z)
>>> print z
255
>>> z = socket.htons(z)
>>> print z
65280

Any thoughts on this would be greatly appreciated, hopefully its just me doing something very wrong.

Edit:

>>> print sys.byteorder
little
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:38:08+00:00Added an answer on May 26, 2026 at 5:38 pm

    Everything looks to be working like it should. Even in your example, the 2 bytes are being swapped, then back again. If the system byte order is the same as the network, then its a no-op, but your question states that the network bytes order is Big, and the host is Little, so the swapping is what it should be doing.

    In [1]: a = 0xECE4
    In [2]: b = 0xE4EC
    
    In [3]: a
    Out[3]: 60644
    
    In [4]: b
    Out[4]: 58604
    
    In [5]: socket.htons(a)
    Out[5]: 58604
    
    In [6]: socket.htons(b)
    Out[6]: 60644
    
    In [7]: socket.htons(a) == b
    Out[7]: True
    
    In [8]: socket.htons(b) == a
    Out[8]: True
    
    In [9]: hex(socket.htons(a))
    Out[9]: '0xe4ec'
    
    In [10]: hex(socket.htons(b))
    Out[10]: '0xece4'
    
    In [11]: bin(socket.htons(a))
    Out[11]: '0b1110010011101100'
    
    In [12]: bin(socket.htons(b))
    Out[12]: '0b1110110011100100'
    

    From the python socket reference:

    socket.htons(x)
    Convert 16-bit positive integers from host to network
    byte order. On machines where the host byte order is the same as
    network byte order, this is a no-op
    ; otherwise, it performs a 2-byte
    swap
    operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking into some XSS prevention in my Java application. I currently have custom
We're currently looking into doing some performance tweaking on a website which relies heavily
Currently looking into learn new technology and silverlight is on the potential list. However,
I was currently looking into memcached as way to coordinate a group of server,
I'm currently looking into unit testing for a new application I have to create.
I am currently looking into spliting a very long string that could contain HTML
I am currently looking into color manipulation / selection etc and have come across
I am looking into improving the backup process a group of animators use. Currently
I'm looking into the various ways to validate input for an editable JComboBox. Currently,
I'm currently looking at different solutions getting 2 dimensional mathematical formulas into webpages. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.