top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

socket data sending problem in python

0 votes

im trying to do a simple socket test program for a school project using the socket module, but im having difficulty in sending data between the client and host program.

so far all tutorials and examples have used something along the lines of:

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 host = socket.gethostname()
 port = 12345
 s.connect((host, port))

and received it on the server end with:

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 host = ''
 port = 12345
 s.bind((host, port))
 conn, addr = s.accept()
 print ('client is at', addr)
 data = conn.recv(5)

it all works fine, except for when i try to use:


to send data between the client and server, i just get this error message:

 Traceback (most recent call last):
 File "C:/Users/xxxxx/Documents/code/chatroom/", line 9, in 
 TypeError: 'str' does not support the buffer interface

if anyone can either show me what im doing wrong, what this means and what's causing it, or even better how to fix it it would be greatly appreciated

posted Jul 3, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Are you using Python 3.x? Try sending bytes instead of strings:


Or switch to using Python 2.7

answer Jul 3, 2013 by anonymous
0 votes

You didn't say which version of Python you're using, but I think that you're using Python 3.

A socket handles bytes, not Unicode strings, so you need to encode the Unicode strings to bytes before sending, and decode the bytes to Unicode strings after receiving.

answer Jul 3, 2013 by anonymous
Similar Questions
+2 votes

As per my understanding Receive system calls will be a blocking call of the code... it will not go ahead unless it receives the response from client .. my question is how would i set time in that? i want my code should wait only for few seconds then it has to come out from that

+2 votes

The problem description:

There are set of processes running on my system say process_set_1. Build a process agent which runs an asynchronous socket listening to incoming requests from process_set_1. Each process sends an id. The agent stores these ids in a dictionary and sends response that ur id has been accepted. Now agent process receives some data from another process (which is some sort of sister process for the agent). This data contains id along with a command which is to be sent by the agent to the process_set_1 through HTTP client over AF_UNIX socket, since each process in process_set_1 has a HTTP listening CLI. The process agent sends an HTTP request by recognizing id stored in dictionary to the process_set_1. A service running in the process_set_1 routes this HTTP command to the respective process.

Now my problem is the HTTP request which to be sent must go through AF_UNIX socket. I got this solution.

class UnixStreamHTTPConnection(httplib.HTTPConnection):

    def __init__(self, path, host='localhost/rg_cli',port=None, strict=None,
        httplib.HTTPConnection.__init__(self, host, port=port, strict=strict,

    def connect(self):
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

But this cannot work since normal socket will not work and thus i thought of asyncore module in python. To use asyncore module again i will have to subclass asyncore.dispatcher. This class also contains connect() method.

Another problem is I don't know how asyncore module works and thus not able to find a way to mix the work of 1) listening forever, accept the connection, store id and the sock_fd.
2) accept data from the process' agent sister process, retrieve the sock_fd by matching the id in the dictionary and send it through the AF_UNIX socket.

Please help since i have already spent 2 days digging it out. Sorry if could explain my problem very well.

0 votes

I am trying to make an HTTPS connection and read that HTTPS support is only available if the socket module was compiled with SSL support.

Can someone elaborate on this? Where can I get the socket module for HTTPS, or how do I build one if I have to?

0 votes

For testing purposes I want my code to raise a socket "connection reset by peer" error, so that I can test how I handle it, but I am not sure how to raise the error.

+1 vote

I remember to avoiding to confusing compiler for header, use

// in my include file
#define MYHEADER_H
// and in others code use:
#ifndef MYHEADER_H
#include blahblah

Unfortunately, I prevent to same error, double import to python, but I don't know how to solve, Please help.