Jump to content

Recommended Posts

Hi there,

 

I have been having a go at creating a socket server in Python and I am a bit stuck, here's my code:

# Created by: Jeremy Smith
# Date: 04/11/2011 @ 16:54

# Illustration taken from: http://effbot.org/librarybook/socket.htm

## Firstly we import the much needed libs:
import socket
import struct, time

# Now define the port no the clients will need to connect to (need to open this port on any NAT or Software based Firewall):
PORT = 8045; # so will technically be like http://192.168.0.2:8045 if it was a http server!

# Now create the socket and get it listening on that port:
service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
service.bind(("", PORT))
service.listen(1)

print "Socket has been created and is listening on port: ", PORT


while 1: # while true?
  channel, info = service.accept()
  print "Received a connection from: ", info
  
  # can i just put in literals within the send method?
  
  channel.send()
  channel.close()

 

The problem I am having is understanding what I can put in the send method, can I just send any thing like in PHP you can use to send things like "server is alive or something", which would become something along the lines of:

 

channel.send("server is alive")

 

Or am I thinking about this the wrong way completely?

 

Any help is much appreciated,

Jeremy.

Link to comment
https://forums.phpfreaks.com/topic/250449-python-socket-server-question/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.