-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add-rpc-and-recv-command #623
Open
wilsonwang371
wants to merge
22
commits into
littlecodersh:master
Choose a base branch
from
wilsonwang371:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2508139
rpc server
wilsonwang371 68ebed5
update rpc code
wilsonwang371 ab24336
add recv api
wilsonwang371 0f49f82
update readme
wilsonwang371 0366ca8
update rpc
wilsonwang371 1488133
fix bug
wilsonwang371 773ae74
fix rpc issue
wilsonwang371 9d4d9c1
update
wilsonwang371 531394b
remove extra log message
wilsonwang371 a79282c
update rpc server code to enable block mode
wilsonwang371 d7f99c4
add run itchat server script
wilsonwang371 118c619
update script
wilsonwang371 ee46d7e
update script
wilsonwang371 73d6633
update
wilsonwang371 4e3a15b
update
wilsonwang371 161aa61
update
wilsonwang371 7fca236
update
wilsonwang371 b679aac
update
wilsonwang371 c474589
bug fix
wilsonwang371 1ef1a9d
update code
wilsonwang371 ee1dfa4
update
wilsonwang371 55f3f6d
update readme
wilsonwang371 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ test.py | |
itchat.pkl | ||
QR.jpg | ||
.DS_Store | ||
QR.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import base64 | ||
import io | ||
import json | ||
import logging | ||
import threading | ||
import types | ||
|
||
import itchat.returnvalues as rv | ||
import itchat.storage.templates as tpl | ||
import six.moves.xmlrpc_client as cli | ||
import six.moves.xmlrpc_server as rpc | ||
|
||
logger = logging.getLogger('itchat') | ||
|
||
exported_functions = [ | ||
# components.login | ||
'login', | ||
'get_QRuuid', | ||
'get_QR', | ||
'check_login', | ||
'web_init', | ||
'show_mobile_login', | ||
'start_receiving', | ||
'get_msg', | ||
'logout', | ||
|
||
# components.contact | ||
'update_chatroom', | ||
'update_friend', | ||
'get_contacts', | ||
'get_friends', | ||
'get_chatrooms', | ||
'get_mps', | ||
'set_alias', | ||
'set_pinned', | ||
'add_friend', | ||
'get_head_img', | ||
'create_chatroom', | ||
'set_chatroom_name', | ||
'delete_member_from_chatroom', | ||
'add_member_into_chatroom', | ||
|
||
# components.messages | ||
'send_raw_msg', | ||
'send_msg', | ||
'upload_file', | ||
'send_file', | ||
'send_image', | ||
'send_video', | ||
'send', | ||
'revoke', | ||
|
||
# components.hotreload | ||
'dump_login_status', | ||
'load_login_status', | ||
|
||
# components.register | ||
'auto_login', | ||
'configured_reply', | ||
'msg_register', | ||
'run', | ||
|
||
# other functions | ||
'search_friends', | ||
'search_chatrooms', | ||
'search_mps', | ||
'set_logging', | ||
'recv', | ||
] | ||
|
||
|
||
def dump_obj(marshaller, value, write, escape=cli.escape): | ||
if isinstance(value, io.BytesIO): | ||
write('<value><base64>') | ||
encoded = base64.encodebytes(value.getvalue()) | ||
write(encoded.decode('ascii')) | ||
write('</base64></value>') | ||
elif isinstance(value, int): | ||
write('<value><i8>') | ||
write('%d' % value) | ||
write('</i8></value>') | ||
else: | ||
write('<value><string>') | ||
write(escape(json.dumps(value))) | ||
write('</string></value>') | ||
|
||
|
||
def register_types(): | ||
cli.Marshaller.dispatch[tpl.Chatroom] = dump_obj | ||
cli.Marshaller.dispatch[tpl.ContactList] = dump_obj | ||
cli.Marshaller.dispatch[tpl.User] = dump_obj | ||
cli.Marshaller.dispatch[tpl.ChatroomMember] = dump_obj | ||
cli.Marshaller.dispatch[tpl.MassivePlatform] = dump_obj | ||
cli.Marshaller.dispatch[rv.ReturnValue] = dump_obj | ||
cli.Marshaller.dispatch[io.BytesIO] = dump_obj | ||
cli.Marshaller.dispatch[int] = dump_obj | ||
|
||
|
||
def load_rpc(core): | ||
core.start_rpc_server = start_rpc_server | ||
|
||
|
||
def start_rpc_server(self, host, port, block=False): | ||
server = rpc.SimpleXMLRPCServer((host, port), logRequests=False, allow_none=True) | ||
server.register_introspection_functions() | ||
for i in exported_functions: | ||
if hasattr(self, i): | ||
server.register_function(getattr(self, i)) | ||
logger.info('Starting RPC server') | ||
self.rpc = server | ||
if not block: | ||
rpc_thread = threading.Thread(target=server.serve_forever, args=()) | ||
rpc_thread.daemon = True | ||
register_types() | ||
rpc_thread.start() | ||
else: | ||
register_types() | ||
server.serve_forever() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env bash | ||
|
||
HOST=localhost | ||
PORT=9000 | ||
QRCODE=True | ||
|
||
usage() { echo "Usage: $0 [-h <HOST_IP> ] [-p <PORT>] [-2]" 1>&2; exit 1; } | ||
|
||
while getopts ":h:p:2" o; do | ||
case "${o}" in | ||
h) | ||
HOST=${OPTARG} | ||
;; | ||
p) | ||
PORT=${OPTARG} | ||
;; | ||
2) | ||
QRCODE=2 | ||
;; | ||
*) | ||
usage | ||
;; | ||
esac | ||
done | ||
shift $((OPTIND-1)) | ||
|
||
cat << EOF | python3 | ||
import sys | ||
import itchat | ||
|
||
def quit(): | ||
print('error, exiting...') | ||
sys.exit(-1) | ||
|
||
if __name__ == '__main__': | ||
try: | ||
itchat.auto_login(enableCmdQR=${QRCODE}, hotReload=True, exitCallback=quit) | ||
itchat.start_rpc_server('${HOST}', ${PORT}, block=True) | ||
except KeyboardInterrupt: | ||
print('exiting...') | ||
sys.exit(0) | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
‘
=>'