I'm trying to create a 2 way communication socket in python. The socket will listen for connections from the client until it gets the data formatted in string like this: 'PHONENUMBER|STATUS'. I will need to split that and convert it to json which will be sent via http request to a web api. When the api receives, it will handle the request and then it will return a status in json format which I will convert to string to return it to the client as string. I would like to have a professional opinion about my code and any additional ideas to what I would like to achieve would be appreciated.

import socket
import json
import requests
import httplib

HOST= ''
PORT= 80

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Bind to adress

print ("Socket bound.")

#Start listening
print ("Socket Listening")

conn, addr = s.accept()
url = 'http://www.google.com'
while True:
      data = conn.recv(1024)

      dictionary = dict(item.split("|") for item in data .split(";"))

      json = json.dumps(dictionary, ensure_ascii=False)

      r = requests.get(url, data=json)

 print r.text
  • \$\begingroup\$ Hi Sam, you are right, I posted this on Stackoverflow and it was formatted in the wrong way. I don't need the first split, that was my error, since the dictionary takes item and split it himself. the print at the end should not be like that. I explained what I intend to do, I added the code just as a reference or idea of what Im trying to achieve. I expect to have some sort of idea besides my own on how to implement my goal. Thanks for the suggestions! \$\endgroup\$ – Alexander P. Feb 3 '15 at 13:42
  • \$\begingroup\$ I changed my comment to an "answer". \$\endgroup\$ – Sam Watkins Feb 3 '15 at 13:43
  • \$\begingroup\$ @AlexDev4Life You should post a working piece of code at least, the errors are minor buy you should still fix them. \$\endgroup\$ – Ashwini Chaudhary Feb 3 '15 at 13:49
  • \$\begingroup\$ Hi, Sorry everyone, I made some fixes on the code to make it clear. I posted this last night really quick. Thanks \$\endgroup\$ – Alexander P. Feb 3 '15 at 14:25
  • \$\begingroup\$ hi, in python indentation is significant. Your last line print r.text needs to be indented same as the previous lines with six spaces (which is an unusual indent level, by the way - I suggest 4 or 8 spaces, or one tab). If this code works now, as you suggest, please provide an example session using e.g. telnet or a test program: what data you send to it, and what data you expect it to send back after talking to google. \$\endgroup\$ – Sam Watkins Feb 4 '15 at 11:04

This code does not compile, and has many bugs. Can you fix the errors in it?

url- = is a syntax error.

I don't recommend calling a variable split when you are using a method split, it's confusing.

You did not use that variable named split.

You did not define a variable message.

print 'r.text' should I suppose be print r.text.

I'm not sure about data = conn.recv(1024), what will happen if a request is longer than 1024 bytes?

This code needs to be debugged, it is not ready for code review. But it seems like a nice idea, and structurally okay, a good start. I could help you debug it and get it working if you like.

  • \$\begingroup\$ Thanks, I formatted the text, it was all messed up. Now it can be reviewed \$\endgroup\$ – Alexander P. Feb 3 '15 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.