Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Need help with a script filter_list
Author
Message
Need help with a script #1
Hello!
I have been sitting on a script for a multi-thread server for a while.
I have no idea how to change the bold bit of code, co it will work.
The script is taken from a tutorial, bu the tutorial was not completely working.
The highlighted part is the one that does not work. Server starts up normally, but when I try connecting, it does not work Sad
Any ideas what I should do?
I know that the client_thread is not defined anywhere, but I have no idea what to put instead. I am on Python 3.2
Code:
import socket
import threading
import _thread
print('Starting server...')
print('Setting up details...')
HOST = 'localhost'
PORT = 8050
BUFSIZ = 1024
ADDR = (HOST, PORT)
print('Setting up listener...')
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)
while 1:
    print('Done! Waiting for connections...')
    clientsocket = serversock.accept()
    print('Connected from', clientsocket)
[u][b]    ct = client_thread(clientsocket)[/b][/u]
    ct.run()

print('Closing Thread')
serversock.close()

Any help would be appreciated. Thank you

Reply

RE: Need help with a script #2
Threads require a target, in this case, you have no defenition for clientsocket, furthermore, looks like there are some bad programming practices in general here...

Code:
from socket import socket, AF_INET, SOCK_STREAM #importing only certain methods or classes makes your program more memory efficient.
from threading import Thread #also not sure what _thread is....

print("Details n' shit...")
HOST = '' #localhost and '' should do the same thing
PORT = 8050
BUFFER_SIZE = 1024 #standard convention for constants is to use the whole name
ADDRESS = (HOST,PORT)

print("wait for connection, and do something with it")
sock = socket(AF_INET, SOCK_STREAM) #if you imported these, you don't need to say socket.___
sock.bind(ADDRESS)
print("Done with setup")
sock.listen(1) #1, 2 what does it matter...

def handelConnection(conn, addr): #Do whatever the connection is supposed to do here
    conn.send(b"Hello, dear sir!") #The b' means bytes, sockets can only send bytes in and out
    conn.close() #I'm handeling closeing the connection here, because it would be easier then setting up a lock or join for the thread

while(True):
    #Why are you printing here?! you'll get spammed with prints! print up there ^^^
    conn, addr = sock.accept()
    thread = Thread(target=handelConnection, args=(conn, addr)) #Since I'm going to assume you know threads, but not python, here we go
    #Threads need a target, a method to run when their start() gets called, if the method requires parameters, the args parameter takes a
    #tuple to pass onto the method.
    thread.start() #actually starts the thread, the thread should handle closing itself on it's own.

print("Holy shit, since when did true become false?")
Typed up on the fly, not tested, you should be able to debug it pretty easy though

Reply

RE: Need help with a script #3
Thanks! It works as it should Biggrin
I have one more question, how can I make my server answer when someone writes something in the client (I mean like netcat), like a command..
For example, client connects to server:
Server:Welcome!
Client: ffddf
Server: Command not spotted etc.

I just need help with setting up this small transaction between client & server. The command can I set up by myself.

Thanks again for this script Tongue I've learned alot from looking through it Biggrin

Reply

RE: Need help with a script #4
My pleasure,
For commands, look at what your handel connection method is actually doing

Code:
def joke():
    return "I really hate this damned machine\nI really wish they'd sell it\nIt never does quite what I want\nbut only what I tell it!"

def anotherCommand():
    file = open("Hello", 'w')
    file.write("anotherCommand makes a file!")
    file.close()
    print("Someone did a command!")
    return "You just did a command!"

#Reason number ___ python is awesome: you can make arrays of methods with strings as indexes
commandlist = {}
commandlist["Joke"] = joke #Does the joke method
commandlist["AnotherCommand"] = anotherCommand #Does the anotherCommand method

def clientHandel(conn, addr)
    data = conn.recv(1024) #This waits till the connection has sent something, and stores it in data
    string = str(data, 'UTF-8') #Python sends bytes over, so we need to convert it to a string
    try:
        message = commandlist[string]()
    except:
        message = 'Failed to do command!'

    outputBytes = bytes(message, 'UTF-8') # need to turn it back into bytes before sending it
    conn.send(outputBytes) #Sends a message to the client

Reply

RE: Need help with a script #5
Thank you again Biggrin
I have no time to test it now though, but I will test it after the new year Smile
It looks like you know alot about python Smile

Reply







Users browsing this thread: 1 Guest(s)